﻿//using Word = Microsoft.Office.Interop.Word;
using System.Web;
using System;
using RealEstate.MVC.DanhMuc.Model;
using RealEstate.MVC.DuAn.Model;
using RealEstate.MVC.Web.Helper;
using RealEstate.MVC.KinhDoanh.Model;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using System.Web.Mail;
using System.Net.Mail;
using System.Text;
using System.Configuration;
using System.Web.Configuration;
using System.Net.Configuration;
using System.Collections.Generic;
using RealEstate.MVC.Common;
namespace RealEstate.MVC.Web.Helper
{
    public delegate string FunChange(string rtf);
    public delegate string DeleFillData(string rtf, object data);
    public delegate string DeleFillLoop(string rtf, object data);

    public static class WordUltities
    {
        #region Object Const Commented

        //static object oMissing = System.Reflection.Missing.Value;
        //static object oVisible = false;
        //static object oReadOnly = false;
        //static object oShowConvention = false;
        //static object oOpenandRepair = true;
        //static object oTrue = true;
        //static object oFalse = false;

        //#endregion

        //#region Fields
        //private static Word.ApplicationClass _wApp = null;
        ///// <summary>
        ///// App chính đang thực thi của word
        ///// </summary>
        //public static Word.ApplicationClass WordApplication
        //{
        //    get { return _wApp ?? (_wApp = new Word.ApplicationClass()); }
        //}

        //private static Word.Application _tApp = null;
        ///// <summary>
        ///// App tạm thời, dùng để sao chép dữ liệu qua app chính
        ///// </summary>
        //public static Word.Application TempApplication
        //{
        //    get { return _tApp ?? (_tApp = new Word.Application()); }
        //}

        //private static Word.Document _wDoc = null;

        //private static string srcFileName;

        //public static string SrcFileName
        //{
        //    get { return srcFileName; }
        //    set { srcFileName = value; }
        //}
        //private static string desFileName = @"C:\Hopdong.doc";

        //public static string DesFileName
        //{
        //    get { return desFileName; }
        //    set { desFileName = value; }
        //}
        //private static string reRultPlainText;

        //public static string ReRultPlainText
        //{
        //    get { return reRultPlainText; }
        //    set { reRultPlainText = value; }
        //}
        //private static string reSultRTF;

        //public static string ReSultRTF
        //{
        //    get { return reSultRTF; }
        //    set { reSultRTF = value; }
        //} 
        #endregion

        #region Methods commented

        //#region Application
        ///// <summary>
        ///// Khởi động word
        ///// </summary>
        ///// <returns></returns>
        //public static bool OpenWordApplication()
        //{
        //    if (WordApplication.Visible == false)
        //        WordApplication.Visible = true;
        //    return true;
        //}

        ///// <summary>
        ///// Đóng word
        ///// </summary>
        ///// <param name="saveChanges"></param>
        ///// <returns></returns>
        //public static bool CloseWordApplication(bool saveChanges)
        //{
        //    object oSave = saveChanges;
        //    Word._Application app = _wApp;
        //    WordApplication.Visible = false;
        //    app.Quit(ref oFalse, ref oMissing, ref oMissing);
        //    _wApp = null;
        //    return true;
        //}


        //#endregion

        //#region Document
        ///// <summary>
        ///// Sao chép nội dung 1 file doc 
        ///// sang nội dung chính
        ///// </summary>
        ///// <returns>Chuỗi định dạng RTF của file</returns>
        //static string CopyContent()
        //{
        //    WordApplication.Selection.WholeStory();
        //    WordApplication.Selection.Copy();
        //    //var xmlFormat = WordApplication.Selection.Document.WordOpenXML;
        //    var xmlFormat = WordApplication.Selection.Document.Content.get_XML(false);
        //    //WordApplication.Selection.Document.PrintOut(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
        //    //string content = (string)Clipboard.GetData("Rich Text Format");
        //    //ReSultRTF = content;
        //    ReRultPlainText = xmlFormat;
        //    CloseDocument(true);
        //    return xmlFormat;
        //}
        //public static void ChangeContain(string content)
        //{
        //    //Clipboard.Clear();
        //    //Clipboard.SetData("Rich Text Format", content);
        //    CreateDocument();
        //    //WordApplication.Selection.Paste();            
        //}
        //public static bool EndWork()
        //{
        //    WordApplication.Quit(ref oFalse, ref oMissing, ref oMissing);
        //    _wApp = null;
        //    return true;
        //}

        ///// <summary>
        ///// Mở một tài liệu trong word
        ///// </summary>
        //public static string OpenDocument()
        //{
        //    object filen = SrcFileName;
        //    _wDoc = WordApplication.Documents.Open(ref filen, ref oMissing, ref oMissing, ref oMissing,
        //         ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        //         ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
        //    WordApplication.Visible = false;
        //    return CopyContent();
        //}

        ///// <summary>
        ///// Tạo một tài liệu mới
        ///// </summary>
        ///// <returns></returns>
        //public static Word.Document CreateDocument()
        //{
        //    _wDoc = WordApplication.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
        //    return _wDoc;
        //}

        ///// <summary>
        ///// Đóng tài liệu hiện thời
        ///// </summary>
        ///// <param name="saveChanges"></param>
        ///// <returns></returns>
        //public static Word.Document CloseDocument(bool saveChanges)
        //{
        //    object oSave = saveChanges;
        //    Word._Document doc = _wDoc;
        //    doc.Close(ref oSave, ref oMissing, ref oMissing);
        //    return _wDoc;
        //}

        ///// <summary>
        ///// Lưu tài liệu
        ///// </summary>
        ///// <returns></returns>
        //public static Word.Document Save()
        //{
        //    _wDoc.Save();
        //    return _wDoc;
        //}

        ///// <summary>
        ///// Lưu với tên file khác
        ///// </summary>
        ///// <param name="fileName"></param>
        ///// <returns></returns>
        //public static Word.Document SaveAs(string fileName)
        //{
        //    object filen = fileName;
        //    _wDoc.SaveAs(ref filen, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        //        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        //        ref oMissing, ref oMissing, ref oMissing);
        //    return _wDoc;
        //}
        //#endregion

        //#region Edit
        //public static bool ReplaceText(string findText, string replaceText, bool replaceAll)
        //{
        //    // lấy tham số
        //    object obh = Word.WdDefaultTableBehavior.wdWord9TableBehavior;
        //    object oaf = Word.WdAutoFitBehavior.wdAutoFitFixed;
        //    object omove = Word.WdMovementType.wdMove;
        //    object ounitS = Word.WdUnits.wdScreen;
        //    object otableBh = Word.WdDefaultTableBehavior.wdWord9TableBehavior;
        //    object otableAf = Word.WdAutoFitBehavior.wdAutoFitFixed;
        //    object ounitCl = Word.WdUnits.wdCell;
        //    object ounitL = Word.WdUnits.wdLine;
        //    object ounitC = Word.WdUnits.wdCharacter;
        //    object ounitSep = Word.WdTableFieldSeparator.wdSeparateByTabs;
        //    object ont = true;
        //    object ofalse = false;
        //    object ocount = 3;
        //    object oreplace;
        //    if (replaceAll)
        //        oreplace = Word.WdReplace.wdReplaceAll;
        //    else
        //        oreplace = Word.WdReplace.wdReplaceOne;
        //    object ofindtext = findText;
        //    object orpltext = replaceText;
        //    WordApplication.Selection.Find.Text = findText;
        //    WordApplication.Selection.Find.Replacement.Text = replaceText;
        //    WordApplication.Selection.Find.Forward = true;
        //    WordApplication.Selection.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
        //    WordApplication.Selection.Find.MatchCase = false;


        //    return WordApplication.Selection.Find.Execute(ref ofindtext, ref oTrue, ref oMissing, ref oMissing,
        //        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref orpltext,
        //        ref oreplace, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

        //}

        ///// <summary>
        ///// thêm một nội dung của rtf vào word tại vị trí hiện tại của con trỏ
        ///// </summary>
        ///// <param name="rtfText"></param>
        ///// <returns></returns>
        //public static void InsertRtf(string rtfText)
        //{
        //    // xóa dữ liệu clipboard
        //    //Clipboard.Clear();
        //    //Clipboard.SetData("Rich Text Format", rtfText);

        //    // dán lên doc
        //    WordApplication.Selection.Paste();
        //}

        //public static void InsertText(string text)
        //{
        //    WordApplication.Selection.TypeText(text);
        //}

        //public static void TypeBackscape()
        //{
        //    WordApplication.Selection.TypeBackspace();
        //}

        //public static void InsertBoldText(string text)
        //{
        //    // lấy format cũ
        //    int old = WordApplication.Selection.Font.Bold;

        //    // đánh với định dạng cho trước
        //    WordApplication.Selection.Font.Bold = 1;
        //    WordApplication.Selection.TypeText(text);

        //    // trả lại format
        //    WordApplication.Selection.Font.Bold = old;
        //}

        ///// <summary>
        ///// Thêm mới 1 bảng vào word
        ///// </summary>
        ///// <param name="row"></param>
        ///// <param name="column"></param>
        ///// <returns>table mới được tạo</returns>
        //public static Word.Table InsertTable(int row, int column)
        //{
        //    object obehavior = Word.WdDefaultTableBehavior.wdWord8TableBehavior;

        //    Word.Table table = _wDoc.Tables.Add(_wApp.Selection.Range, row, column, ref obehavior, ref oMissing);

        //    return table;
        //}

        ///// <summary>
        ///// Chuyển 1 table thành text
        ///// </summary>
        ///// <param name="table"></param>
        ///// <returns></returns>
        //public static bool ConvertTable2Text()
        //{
        //    object oTab = Word.WdTableFieldSeparator.wdSeparateByTabs;
        //    _wApp.Selection.Rows.ConvertToTextOld(ref oTab);
        //    return true;
        //}
        //#endregion

        //#region Move Char


        ///// <summary>
        ///// Di chuyển con trỏ lên
        ///// </summary>
        ///// <param name="step"></param>
        ///// <param name="isSelect"></param>
        ///// <returns></returns>
        //public static int MoveUp(int step, bool isSelect)
        //{
        //    object oChar = Word.WdUnits.wdLine;
        //    object oCount = step;
        //    object oExtend = isSelect ? Word.WdMovementType.wdExtend : Word.WdMovementType.wdMove;

        //    return _wApp.Selection.MoveUp(ref oChar, ref oCount, ref oExtend);
        //}

        ///// <summary>
        ///// Di chuyển con trỏ xuống
        ///// </summary>
        ///// <param name="step"></param>
        ///// <param name="isSelect"></param>
        ///// <returns></returns>
        //public static int MoveDown(int step, bool isSelect)
        //{

        //    object oChar = Word.WdUnits.wdLine;
        //    object oCount = step;
        //    object oExtend = isSelect ? Word.WdMovementType.wdExtend : Word.WdMovementType.wdMove;

        //    return _wApp.Selection.MoveDown(ref oChar, ref oCount, ref oExtend);
        //}

        //public static int MoveDownScreen(int step, bool isSelect)
        //{
        //    _wApp.Selection.TypeParagraph();

        //    object oChar = Word.WdUnits.wdScreen;
        //    object oCount = step;
        //    object oExtend = isSelect ? Word.WdMovementType.wdExtend : Word.WdMovementType.wdMove;

        //    return _wApp.Selection.MoveDown(ref oChar, ref oCount, ref oExtend);
        //}

        ///// <summary>
        ///// Di chuyển con trỏ qua trái
        ///// </summary>
        ///// <param name="step"></param>
        ///// <param name="isSelect"></param>
        ///// <returns></returns>
        //public static int MoveLeft(int step, bool isSelect)
        //{
        //    object oChar = Word.WdUnits.wdCharacter;
        //    object oCount = step;
        //    object oExtend = isSelect ? Word.WdMovementType.wdExtend : Word.WdMovementType.wdMove;

        //    return _wApp.Selection.MoveLeft(ref oChar, ref oCount, ref oExtend);
        //}

        ///// <summary>
        ///// Di chuyển con trỏ qua phải
        ///// </summary>
        ///// <param name="step"></param>
        ///// <param name="isSelect"></param>
        ///// <returns></returns>
        //public static int MoveRight(int step, bool isSelect)
        //{
        //    object oChar = Word.WdUnits.wdCharacter;
        //    object oCount = step;
        //    object oExtend = isSelect ? Word.WdMovementType.wdExtend : Word.WdMovementType.wdMove;

        //    return _wApp.Selection.MoveRight(ref oChar, ref oCount, ref oExtend);
        //}

        ///// <summary>
        ///// Chuyển về đầu hàng
        ///// </summary>
        ///// <param name="isSelect"></param>
        ///// <returns></returns>
        //public static int MoveStartLine()
        //{
        //    object oLine = Word.WdUnits.wdLine;
        //    object oCount = 1;
        //    return _wApp.Selection.MoveStart(ref oLine, ref oCount);
        //}

        ///// <summary>
        ///// Chuyển về cuối hàng
        ///// </summary>
        ///// <param name="isSelect"></param>
        ///// <returns></returns>
        //public static int MoveEndLine()
        //{
        //    object oLine = Word.WdUnits.wdLine;
        //    object oCount = 1;
        //    return _wApp.Selection.MoveEnd(ref oLine, ref oCount);
        //}

        //#endregion

        //#region Move Table
        ///// <summary>
        ///// Chuyển qua trái 1 ô
        ///// </summary>
        ///// <param name="step"></param>
        ///// <returns></returns>
        //public static int MoveLeftCell(int step)
        //{
        //    object ocell = Word.WdUnits.wdCell;
        //    object oCount = step;

        //    return _wApp.Selection.MoveLeft(ref ocell, ref oCount, ref oFalse);
        //}

        ///// <summary>
        ///// Chuyển qua phải 1 ô
        ///// </summary>
        ///// <param name="step"></param>
        ///// <returns></returns>
        //public static int MoveRightCell(int step)
        //{
        //    object ocell = Word.WdUnits.wdCell;
        //    object oCount = step;

        //    return _wApp.Selection.MoveRight(ref ocell, ref oCount, ref oFalse);
        //}
        //#endregion

        //#region Sinh tai lieu
        ///// <summary>
        ///// Lấy dữ liệu của file word theo định dạng XML
        ///// </summary>
        ///// <returns></returns>
        //public static string GetDocumentXML()
        //{
        //    OpenDocument();
        //    EndWork();
        //    return ReRultPlainText;
        //}
        //#endregion

        //#region Helper
        //public static bool CopyTableContent(string fileName)
        //{
        //    object filen = fileName;
        //    // mở file để đọc dữ liệu
        //    Word.Document tmpDoc = TempApplication.Documents.Open(ref filen, ref oMissing, ref oMissing, ref oMissing,
        //         ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        //         ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
        //    object oCell = Word.WdUnits.wdLine;
        //    object oExtend = Word.WdMovementType.wdExtend;
        //    // sao chép nội dung
        //    for (int i = 0; i < 5; i++)
        //        TempApplication.Selection.EndKey(ref oCell, ref oExtend);
        //    TempApplication.Selection.Copy();

        //    Word._Document doc = tmpDoc;
        //    // đóng tài liệu để cho lần copy sau
        //    doc.Close(ref oFalse, ref oMissing, ref oMissing);
        //    return true;
        //}

        //public static int MoveDownCell(int step)
        //{
        //    object ocell = Word.WdUnits.wdCell;
        //    object oCount = step;

        //    return _wApp.Selection.MoveDown(ref ocell, ref oCount, ref oFalse);
        //}

        //public static void InsertRowsBelow(int numRows)
        //{
        //    object oNum = numRows;
        //    _wApp.Selection.InsertRowsBelow(ref oNum);
        //}

        ///// <summary>
        ///// Chuyển về đầu dòng
        ///// </summary>
        //public static void MoveHomePar()
        //{
        //    object oPar = Word.WdUnits.wdParagraph;
        //    object oCount = 1;
        //    _wApp.Selection.MoveStart(ref oPar, ref oCount);
        //}

        //public static void MoveEndPar()
        //{
        //    object oPar = Word.WdUnits.wdParagraph;
        //    object oCount = 1;
        //    _wApp.Selection.MoveEnd(ref oPar, ref oCount);
        //}

        //public static void CreateTextColumn(int colCount)
        //{
        //    if (_wApp.ActiveWindow.View.SplitSpecial != Microsoft.Office.Interop.Word.WdSpecialPane.wdPaneNone)
        //        _wApp.ActiveWindow.Panes[2].Close();
        //    object ostart = _wApp.Selection.Start;
        //    object obreak = Word.WdBreakType.wdSectionBreakContinuous;
        //    _wApp.ActiveDocument.Range(ref ostart, ref ostart).InsertBreak(ref obreak);
        //    _wApp.Selection.Start++;
        //    _wApp.Selection.PageSetup.TextColumns.SetCount(colCount);
        //}

        ///// <summary>
        ///// Ngắt kết nối(sử dụng cho trường hợp xem file)
        ///// </summary>
        //public static void CutConnection()
        //{
        //    _wApp = null;
        //}
        //#endregion

        #endregion

        public static string groupingSymbol
        {
            get;
            set;
        }
        public static string decimalSymbol
        {
            get;
            set;
        }
        public static int decimalPlaces
        {
            get;
            set;
        }

        #region Help Fill Data
        public static string RemoveCodeHTML(this string html)
        {
            if (string.IsNullOrEmpty(html))
                return "";
            while (html.Contains("<"))
            {
                var indexLT = html.IndexOf("<");
                var indexGT = html.IndexOf(">");
                html = html.Remove(indexLT, indexGT - indexLT + 1);
            }
            //return HttpUtility.HtmlEncode(html);
            return HttpUtility.HtmlDecode(html);
        }
        /// <summary>
        /// Điền thông tin cho các loại văn bản
        /// </summary>
        /// <param name="document"></param>
        /// <param name="data"></param>
        /// <param name="FillData"></param>
        /// <param name="FillLoop"></param>
        /// <returns></returns>
        public static string FillDataForObject(string document, object data, DeleFillData FillData, DeleFillLoop FillLoop)
        {
            //return document;
            var result = "";
            var startString = document.Split(new string[] { "StartFor" }, StringSplitOptions.None);
            if (startString.Length >= 1)
            {
                result = FillData(startString[0], data);
                //To do: Dua du lieu vao vong lap
                for (int i = 1; i < startString.Length; i++)
                {
                    var endString = startString[i].Split(new string[] { "EndFor" }, StringSplitOptions.None);
                    //result += endString[0];
                    if (endString != null && endString.Length > 0)
                    {
                        string strLoop = FillLoop(endString[0], data);
                        if (string.IsNullOrEmpty(strLoop))
                        {
                            if (result.Contains("StartTitleFor"))
                            {
                                int indexBR = result.IndexOf("StartTitleFor");
                                int indexER = result.IndexOf("EndTitleFor");
                                result = result.Remove(indexBR, indexER - indexBR + 11);
                            }
                        }
                        else
                        {
                            result.Replace("StartTitleFor", "");
                            result.Replace("EndTitleFor", "");
                            result += strLoop;
                        }
                        result += FillData(endString[1], data);
                    }
                }
            }
            result = result.Replace("&lt;&lt;", "").Replace("&gt;&gt;", "");
            return result;
        }
        /// <summary>
        /// Điền thông tin chủ đầu tư
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillChuDauTu(string str, DAChuDauTuInfo data)
        {
            str = str.Replace("maChuDauTu", data.MaChuDauTu ?? "");
            str = str.Replace("tenChuDauTu", data.TenChuDauTu ?? "");
            string dc = WordUltities.RemoveCodeHTML(data.DiaChi);
            str = str.Replace("diaChiChuDauTu", dc);
            str = str.Replace("dienThoaiChuDauTu", data.DienThoai ?? "");
            str = str.Replace("faxChuDauTu", data.Fax ?? "");
            str = str.Replace("webSiteChuDauTu", data.Website ?? "");
            str = str.Replace("maSoThueChuDauTu", data.MaSoThue ?? "");
            str = str.Replace("nguoiLienHeChuDauTu", data.NguoiLienHe ?? "");
            string gc = WordUltities.RemoveCodeHTML(data.GhiChu);
            str = str.Replace("ghiChuChuDauTu", gc);
            return str;
        }
        /// <summary>
        /// Điền thông tin dự án
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillDuAn(string str, DADuAnInfo data)
        {
            str = str.Replace("maDuAn", data.MaDuAn ?? "");
            string tda = WordUltities.RemoveCodeHTML(data.TenDuAn);
            str = str.Replace("tenDuAn", tda);
            string dd = WordUltities.RemoveCodeHTML(data.DiaDiem);
            str = str.Replace("diaDiemDuAn", dd ?? "");
            
            str = str.Replace("tongDienTichDuAn", StringHelper.Format(data.TongDienTich, groupingSymbol, decimalSymbol, 0));
            str = str.Replace("dienTichSuDungChungDuAn", StringHelper.Format(data.DienTichSuDungChung, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("matDoXayDungDuAn", StringHelper.Format(data.MatDoXayDung, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichSanXayDungDuAn", StringHelper.Format(data.DienTichSanXayDung, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichKinhDoanhDuAn", StringHelper.Format(data.DienTichKinhDoanh, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("tongSoCanHoDuAn", StringHelper.Format(data.TongSoCanHo, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("ngayKhoiCongDuAn", data.NgayKhoiCong != null ? string.Format("{0:dd/MM/yyyy}", data.NgayKhoiCong.Value).DateToText() : "");
            str = str.Replace("ngayHoanThanhDuAn", data.NgayHoanThanh != null ? string.Format("{0:dd/MM/yyyy}", data.NgayHoanThanh.Value).DateToText() : "");
            string dvtk = WordUltities.RemoveCodeHTML(data.DonViThietKe);
            str = str.Replace("donViThietKeDuAn", dvtk);
            string dvtc = WordUltities.RemoveCodeHTML(data.DonViThiCong);
            str = str.Replace("donViThiCongDuAn", dvtc);
            string dvgs = WordUltities.RemoveCodeHTML(data.DonViGiamSat);
            str = str.Replace("donViGiamSatDuAn", dvgs);
            string pl = WordUltities.RemoveCodeHTML(data.PhapLyDuAn);
            str = str.Replace("phapLyDuAn", pl ?? "");
            string gc = WordUltities.RemoveCodeHTML(data.GhiChu);
            str = str.Replace("ghiChuDuAn", gc ?? "");
            str = str.Replace("phuongDuAn", data.phuong ?? "");
            return str;
        }
        /// <summary>
        /// Điền thông tin Block - Khu đất
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillBlock(string str, DABlockInfo data)
        {
            str = str.Replace("maBlock", data.MaBlock ?? "");
            str = str.Replace("tongDienTichBlock", StringHelper.Format(data.TongDienTich, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichSuDungChungBlock", StringHelper.Format(data.DienTichSuDungChung, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("matDoXayDungBlock", StringHelper.Format(data.MatDoXayDung, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichSanXayDungBlock", StringHelper.Format(data.DienTichSanXayDung, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichKinhDoanhBlock", StringHelper.Format(data.DienTichKinhDoanh, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("tongSoCanHoBlock", StringHelper.Format(data.TongSoCanHo, groupingSymbol, decimalSymbol, decimalPlaces));
            string mt = WordUltities.RemoveCodeHTML(data.MoTa);
            str = str.Replace("moTaBlock", mt);
            string gc = WordUltities.RemoveCodeHTML(data.GhiChu);
            str = str.Replace("ghiChuBlock", gc);
            return str;
        }
        /// <summary>
        /// Điền thông tin Tầng - Lô đất
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillTang(string str, DALoTangInfo data)
        {
            str = str.Replace("maLoTang", data.MaLoTang ?? "");
            str = str.Replace("tongDienTichTang", StringHelper.Format(data.TongDienTich, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichSuDungChungTang", StringHelper.Format(data.DienTichSuDungChung, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("matDoXayDungTang", StringHelper.Format(data.MatDoXayDung, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichSanXayDungTang", StringHelper.Format(data.DienTichSanXayDung, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichKinhDoanhTang", StringHelper.Format(data.DienTichKinhDoanh, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("tongSoCanHoTang", StringHelper.Format(data.TongSoCanHo, groupingSymbol, decimalSymbol, decimalPlaces));
            string mt = WordUltities.RemoveCodeHTML(data.MoTa);
            str = str.Replace("moTaTang", mt);
            string gc = WordUltities.RemoveCodeHTML(data.GhiChu);
            str = str.Replace("ghiChuDuAn", gc);
            return str;
        }
        /// <summary>
        /// Điền thông tin sản phẩm
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillSanPham(string str, DANenCanHoInfo data)
        {
            str = str.Replace("maSanPham", data.MaNenCanHo ?? "");
            str = str.Replace("tenSanPham", data.MaNenCanHo ?? "");
            str = str.Replace("loaiSanPham", data.LoaiStr);
            str = str.Replace("huongSanPham", data.HuongStr ?? "");
            str = str.Replace("thuaDatSanPham", data.ThuaDat);
            str = str.Replace("toBanDoSanPham", data.ToBanDo);
            str = str.Replace("chieuDaiSanPham", StringHelper.Format(data.ChieuDai, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("chieuRongSanPham", StringHelper.Format(data.ChieuRong, groupingSymbol, decimalSymbol, decimalPlaces));
            var dienTich = data.DienTich.HasValue ? data.DienTich.Value : 0;
            var dienTichXD = data.DienTichXayDung.HasValue ? data.DienTichXayDung.Value : 0;
            str = str.Replace("dienTichSanPham", StringHelper.Format(dienTich, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichXayDungSanPham", StringHelper.Format(dienTichXD, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichSuDungChungSanPham", StringHelper.Format(data.DienTichSuDungChung, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichSuDungRiengSanPham", StringHelper.Format(data.DienTichSuDungRieng, groupingSymbol, decimalSymbol, decimalPlaces));
            var donGia = data.DonGiaXayDung.HasValue ? data.DonGiaXayDung.Value : (data.DonGia.HasValue ? data.DonGia.Value : 0);
            str = str.Replace("donGiaBangChuSanPham", StringHelper.ReadNumber(StringHelper.Format(donGia, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("donGiaSanPham", StringHelper.Format(donGia, groupingSymbol, decimalSymbol, 0));
            var triGiaNen = data.TriGiaNen.HasValue ? data.TriGiaNen.Value : 0;
            str = str.Replace("giaTriNenBangChuSanPham", StringHelper.ReadNumber(StringHelper.Format(triGiaNen, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("giaTriNenSanPham", StringHelper.Format(triGiaNen, groupingSymbol, decimalSymbol, 0));
            var triGiaTho = data.TriGiaTho.HasValue ? data.TriGiaTho : 0;
            str = str.Replace("giaTriThoBangChuSanPham", StringHelper.ReadNumber(StringHelper.Format(triGiaTho, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("giaTriThoSanPham", StringHelper.Format(triGiaTho, groupingSymbol, decimalSymbol, 0));
            var triGiaHoanThien = data.TriGiaHoanThien.HasValue ? data.TriGiaHoanThien.Value : 0;
            str = str.Replace("giaTriHoanThienBangChuSanPham", StringHelper.ReadNumber(StringHelper.Format(triGiaHoanThien, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("giaTriHoanThienSanPham", StringHelper.Format(triGiaHoanThien, groupingSymbol, decimalSymbol, 0));
            str = str.Replace("tyLeHoaHongSanPham", StringHelper.Format(data.TyLeHoaHong, groupingSymbol, decimalSymbol, decimalPlaces));
            var tienHoaHong = data.TienHoaHong.HasValue ? data.TienHoaHong.Value : 0;
            str = str.Replace("tienHoaHongBangChuSanPham", StringHelper.ReadNumber(StringHelper.Format(tienHoaHong, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("tienHoaHongSanPham", StringHelper.Format(tienHoaHong, groupingSymbol, decimalSymbol, 0));
            str = str.Replace("phiBaoTriSanPham", StringHelper.Format(data.PhiBaoTri, groupingSymbol, decimalSymbol, decimalPlaces));
            var tienBaoTri = data.TienBaoTri.HasValue ? data.TienBaoTri.Value : 0;
            str = str.Replace("tienBaoTriBangChuSanPham", StringHelper.ReadNumber(StringHelper.Format(tienBaoTri, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("tienBaoTriSanPham", StringHelper.Format(tienBaoTri, groupingSymbol, decimalSymbol, 0));
            string mt = WordUltities.RemoveCodeHTML(data.MoTa);
            str = str.Replace("moTaSanPham", mt);
            string gcsp = WordUltities.RemoveCodeHTML(data.GhiChu);
            str = str.Replace("ghiChuSanPham", gcsp);
            str = str.Replace("soGCNSanPham", data.SoGCN ?? "");
            str = str.Replace("ngayCapSanPham", data.NgayCap != null ? string.Format("{0:dd/MM/yyyy}", data.NgayCap).DateToText() : "");
            str = str.Replace("coQuanCapSanPham", data.CoQuanCap ?? "");
            if (data.BlockInfo != null)
                str = WordUltities.FillBlock(str, data.BlockInfo);
            if (data.LoTangInfo != null)
                str = WordUltities.FillTang(str, data.LoTangInfo);
            return str;
        }
        /// <summary>
        /// Điền thông tin giai đoạn bàn giao
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillGiaiDoanBanGiao(string str, DAGiaiDoanBanGiaoInfo data)
        {
            str = str.Replace("maBanGiao", data.MaBanGiao.ToString());
            str = str.Replace("ngayBanGiao", data.NgayBanGiaoStr.DateToText());
            str = str.Replace("thoiHanBanGiao", data.ThoiHan == null ? data.ThoiHan.Value.ToString() : "");
            string gc = WordUltities.RemoveCodeHTML(data.GhiChu);
            str = str.Replace("ghiChuBanGiao", gc);
            return str;
        }
        /// <summary>
        /// Điền thông tin phương thức thanh toán
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillPhuongThucThanhToan(string str, DAPhuongThucThanhToanInfo data)
        {
            str = str.Replace("tenPhuongThuc", data.TenPhuongThuc);
            str = str.Replace("tyLeChietKhauPhuongThuc", StringHelper.Format(data.TyLeChietKhau, groupingSymbol, decimalSymbol, decimalPlaces));
            string gc = WordUltities.RemoveCodeHTML(data.GhiChu);
            str = str.Replace("ghiChuPhuongThuc", gc);
            return str;
        }
        /// <summary>
        /// Điền thông tin về giai đoạn
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillGiaiDoan(string str, KDBHopDongThanhToanInfo data)
        {
            str = str.Replace("tenGiaiDoan", data.TenGiaiDoan);
            str = str.Replace("ngayDuKien", string.Format("{0:dd/MM/yyyy}", data.NgayDuKien).DateToText());
            str = str.Replace("tyLeThanhToan", StringHelper.Format(data.TyLeThanhToan, groupingSymbol, decimalSymbol, decimalPlaces));
            if (data.GiaTriThanhToan != 0)
                str = str.Replace("giaTriThanhToanBangChu", StringHelper.ReadNumber(StringHelper.Format(data.GiaTriThanhToan, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("giaTriThanhToanBangChu", "Không ");
            str = str.Replace("giaTriThanhToan", StringHelper.Format(data.GiaTriThanhToan, groupingSymbol, decimalSymbol, 0));
            str = str.Replace("soNgayChoPhepTre", data.ChoPhepTre != null ? data.ChoPhepTre.Value.ToString() : "");
            str = str.Replace("laiSuat", data.LaiSuatTre.Value.ToString());
            str = str.Replace("noiDungThanhToan", data.NoiDung);

            return str;
        }
        /// <summary>
        /// Điền thông tin khách hàng
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillKhachHang(string str, DMKhachHangInfo data)
        {
            str = str.Replace("tenKhachHang", string.IsNullOrEmpty(data.HoTenKhachHang) == false ? data.HoTenKhachHang : "");
            str = str.Replace("ngaySinhKhachHang", data.NgaySinh != null ? string.Format("{0:dd/MM/yyyy}", data.NgaySinh) : "");
            str = str.Replace("gioiTinhKhachHang", string.IsNullOrEmpty(data.GioiTinhStr) == false ? data.GioiTinhStr : "");
            str = str.Replace("quocTichKhachHang", string.IsNullOrEmpty(data.QuocTich) == false ? data.QuocTich : "");
            str = str.Replace("cmndSoKhachHang", string.IsNullOrEmpty(data.CmndSo) == false ? data.CmndSo : "");
            str = str.Replace("cmndNgayCapKhachHang", string.IsNullOrEmpty(data.CmndNgayCapStr) == false ? data.CmndNgayCapStr : "");
            str = str.Replace("cmndNoiCapKhachHang", string.IsNullOrEmpty(data.CmndNoiCap) == false ? data.CmndNoiCap : "");
            string dcc = WordUltities.RemoveCodeHTML(data.DiaChi);
            str = str.Replace("diaChiKhachHang", string.IsNullOrEmpty(dcc) == false ? dcc : "");
            string tt = WordUltities.RemoveCodeHTML(data.ThuongTru);
            str = str.Replace("thuongTruKhachHang", string.IsNullOrEmpty(tt) == false ? tt : "");
            str = str.Replace("dienThoaiKhachHang", string.IsNullOrEmpty(data.DienThoai) == false ? data.DienThoai : "");
            str = str.Replace("faxKhachHang", string.IsNullOrEmpty(data.Fax) == false ? data.Fax : "");
            str = str.Replace("diDongKhachHang", string.IsNullOrEmpty(data.DiDong) == false ? data.DiDong : "");
            str = str.Replace("emailKhachHang", string.IsNullOrEmpty(data.Email) == false ? data.Email : "");
            str = str.Replace("taiKhoanKhachHang", string.IsNullOrEmpty(data.TaiKhoan) == false ? data.TaiKhoan : "");
            str = str.Replace("ngayHienHanh", DateTime.Now.Day.ToString().PadLeft(2, '0'));
            str = str.Replace("thangHienHanh", DateTime.Now.Month.ToString().PadLeft(2, '0'));
            str = str.Replace("namHienHanh", DateTime.Now.Year.ToString());
            if (data.NganHangInfo != null)
            {
                str = str.Replace("nganHangKhachHang", string.IsNullOrEmpty(data.NganHangInfo.TenNganHang) == false ? data.NganHangInfo.TenNganHang : "");
            }
            else
            {
                str = str.Replace("nganHangKhachHang", "");
            }
            str = str.Replace("maSoThueKhachHang", string.IsNullOrEmpty(data.MaSoThue) == false ? data.MaSoThue : "");
            //str = str.Replace("ngheNghiepKhachHang", string.IsNullOrEmpty(data.NgheNghiep) == false ? data.NgheNghiep : "");
            str = str.Replace("ngheNghiepKhachHang", string.IsNullOrEmpty(data.IdNgheNghiep.ToString()) == false ? data.IdNgheNghiep.ToString() : "");
            return str;
        }
        /// <summary>
        /// Điền thông tin công ty (Thông tin bên A)
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillCongTy(string str, List<DMThongTinCongTyInfo> listData)
        {
            //if (listData != null && listData.Count > 0)
            //{
            //    var data = listData[0];
            //    str = str.Replace("ngayHomNay", string.Format("{0:dd/MM/yyyy}", DateTime.Now).DateToText());
            //    str = str.Replace("maCongTyBenA", data.MaCongTy);
            //    str = str.Replace("tenCongTyBenA", data.TenCongTy);
            //    string dc = WordUltities.RemoveCodeHTML(data.DiaChi);
            //    str = str.Replace("diaChiBenA", dc ?? "");
            //    str = str.Replace("dienThoaiBenA", data.DienThoai);
            //    str = str.Replace("soFaxBenA", data.SoFax);
            //    str = str.Replace("taiKhoanBenA", data.TaiKhoan);
            //    string nh = WordUltities.RemoveCodeHTML(data.NganHang);
            //    str = str.Replace("nganHangBenA", nh);
            //    str = str.Replace("maSoThueBenA", data.MaSoThue);
            //    str = str.Replace("nguoiDaiDienBenA", data.NguoiDaiDien);
            //    str = str.Replace("chucVuDaiDienBenA", data.ChucVuDaiDien);
            //}
            //else
            //{
            //    str = str.Replace("ngayHomNay", "");
            //    str = str.Replace("maCongTyBenA", "");
            //    str = str.Replace("tenCongTyBenA", "");
            //    str = str.Replace("diaChiBenA", "");
            //    str = str.Replace("dienThoaiBenA", "");
            //    str = str.Replace("soFaxBenA", "");
            //    str = str.Replace("taiKhoanBenA", "");
            //    str = str.Replace("nganHangBenA", "");
            //    str = str.Replace("maSoThueBenA", "");
            //    str = str.Replace("nguoiDaiDienBenA", "");
            //    str = str.Replace("chucVuDaiDienBenA", "");
            //}
            return str;
        }
        /// <summary>
        /// Điền thông tin phiếu giữ chỗ
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillPhieuGiuCho(string str, KDBGiuChoInfo data, bool md)
        {
            str = str.Replace("soPhieuGiuCho", data.MaGiuCho);
            str = str.Replace("ngayDangKyGiuCho", data.NgayDangKyStr.DateToText());
            str = str.Replace("loaiSanPhamGiuCho", data.LoaiNenCanHoStr);
            str = str.Replace("ngayBocThamGiuCho", data.NgayBocThamStr.DateToText());
            string ndck = WordUltities.RemoveCodeHTML(data.NoiDungCamKet);
            str = str.Replace("noiDungCamKetGiuCho", ndck);
            string gc = WordUltities.RemoveCodeHTML(data.GhiChu);
            str = str.Replace("ghiChuGiuCho", gc);
            if (md == true)
            {
                if (data.DuAnInfo != null)
                    str = WordUltities.FillDuAn(str, data.DuAnInfo);
                if (data.BlockInfo != null)
                    str = WordUltities.FillBlock(str, data.BlockInfo);
                if (data.LoTangInfo != null)
                    str = WordUltities.FillTang(str, data.LoTangInfo);
                if (data.NenCanHoInfo != null)
                    str = WordUltities.FillSanPham(str, data.NenCanHoInfo);
                if (data.KhachHangList != null)
                {
                    foreach (var kh in data.KhachHangList)
                    {
                        if (kh.Id == data.DaiDien)
                        {
                            str = WordUltities.FillKhachHang(str, kh);
                            break;
                        }
                    }
                }
            }
            return str;
        }
        /// <summary>
        /// Điền thông tin cho phiếu đăng ký
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillPhieuDangKy(string str, KDBDangKyInfo data, bool md)
        {
            str = str.Replace("soDangKy", data.MaDangKy);
            str = str.Replace("ngayDangKy", data.NgayDangKyStr.DateToText());
            str = str.Replace("loaiSanPham", data.LoaiNenCanHoStr);

            var dienTich = data.DienTichTT.IsNullOrZero() ? (data.DienTich.IsNullOrZero() ? (data.NenCanHoInfo.DienTich.IsNullOrZero() ? 0 : data.NenCanHoInfo.DienTich) : data.DienTich) : data.DienTichTT;
            str = str.Replace("dienTichDangKy", StringHelper.Format(dienTich, groupingSymbol, decimalSymbol, decimalPlaces));

            var dienTichXayDung = data.DienTichXayDungTT.IsNullOrZero() ? (data.DienTichXayDung.IsNullOrZero() ? (data.NenCanHoInfo.DienTichXayDung.IsNullOrZero() ? 0 : data.NenCanHoInfo.DienTichXayDung) : data.DienTichXayDung) : data.DienTichXayDungTT;
            str = str.Replace("dienTichXayDungDangKy", StringHelper.Format(dienTichXayDung, groupingSymbol, decimalSymbol, decimalPlaces));

            var donGia = data.DonGiaTT.IsNullOrZero() ? (data.DonGia.IsNullOrZero() ? (data.NenCanHoInfo.DonGia.IsNullOrZero() ? 0 : data.NenCanHoInfo.DonGia) : data.DonGia) : data.DonGiaTT;
            if (data.DonGia.HasValue)
                str = str.Replace("donGiaBangChuDangKy", StringHelper.ReadNumber(StringHelper.Format(donGia, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("donGiaBangChuDangKy", "");
            str = str.Replace("donGiaDangKy", StringHelper.Format(donGia, groupingSymbol, decimalSymbol, 0));

            var donGiaXayDung = data.DonGiaXayDungTT.IsNullOrZero() ? (data.DonGiaXayDung.IsNullOrZero() ? (data.NenCanHoInfo.DonGiaXayDung.IsNullOrZero() ? 0 : data.NenCanHoInfo.DonGiaXayDung) : data.DonGiaXayDung) : data.DonGiaXayDungTT;
            if (data.DonGiaXayDung.HasValue)
                str = str.Replace("donGiaXayDungBangChuDangKy", StringHelper.ReadNumber(StringHelper.Format(donGiaXayDung, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("donGiaXayDungBangChuDangKy", "");
            str = str.Replace("donGiaXayDungDangKy", StringHelper.Format(donGiaXayDung, groupingSymbol, decimalSymbol, 0));

            var triGiaNen = data.TriGiaNen.IsNullOrZero() ? (data.NenCanHoInfo.TriGiaNen.IsNullOrZero() ? 0 : data.NenCanHoInfo.TriGiaNen) : data.TriGiaNen;
            if (data.TriGiaNen.HasValue)
                str = str.Replace("giaTriNenBangChuDangKy", StringHelper.ReadNumber(StringHelper.Format(triGiaNen, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("giaTriNenBangChuDangKy", "");
            str = str.Replace("giaTriNenDangKy", StringHelper.Format(triGiaNen, groupingSymbol, decimalSymbol, 0));

            if (data.TriGiaNha.HasValue)
                str = str.Replace("giaTriNhaBangChuDangKy", StringHelper.ReadNumber(StringHelper.Format(data.TriGiaNha, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("giaTriNhaBangChuDangKy", "");
            str = str.Replace("giaTriNhaDangKy", StringHelper.Format(data.TriGiaNha, groupingSymbol, decimalSymbol, 0));
            if (data.TongGiaTri.HasValue)
                str = str.Replace("tongGiaTriBangChuDangKy", StringHelper.ReadNumber(StringHelper.Format(data.TongGiaTri, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("tongGiaTriBangChuDangKy", "");
            str = str.Replace("tongGiaTriDangKy", StringHelper.Format(data.TongGiaTri, groupingSymbol, decimalSymbol, 0));
            str = str.Replace("tyLeGiamDangKy", StringHelper.Format(data.TyLeGiam, groupingSymbol, decimalSymbol, decimalPlaces));
            if (data.TienGiam.HasValue)
                str = str.Replace("tienGiamBangChuDangKy", StringHelper.ReadNumber(StringHelper.Format(data.TienGiam, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("tienGiamBangChuDangKy", "");
            str = str.Replace("tienGiamDangKy", StringHelper.Format(data.TienGiam, groupingSymbol, decimalSymbol, 0));
            if (data.GiaTriHopDong.HasValue)
                str = str.Replace("giaTriChuaVATBangChuDangKy", StringHelper.ReadNumber(StringHelper.Format(data.GiaTriHopDong, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("giaTriChuaVATBangChuDangKy", "");
            str = str.Replace("giaTriChuaVATDangKy", StringHelper.Format(data.GiaTriHopDong, groupingSymbol, decimalSymbol, 0));
            str = str.Replace("thueVATDangKy", StringHelper.Format(data.ThueVAT, groupingSymbol, decimalSymbol, decimalPlaces));
            if (data.GiaTriHopDongVAT.HasValue)
                str = str.Replace("giaTriCoVATBangChuDangKy", StringHelper.ReadNumber(StringHelper.Format(data.GiaTriHopDongVAT, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("giaTriCoVATBangChuDangKy", "");
            str = str.Replace("giaTriCoVATDangKy", StringHelper.Format(data.GiaTriHopDongVAT, groupingSymbol, decimalSymbol, 0));
            if (data.ThanhToanList != null && data.ThanhToanList.Count > 0)
            {
                var tt = data.ThanhToanList[0];
                str = str.Replace("tienDatCocBangChuDangKy", StringHelper.ReadNumber(StringHelper.Format(tt.GiaTriThanhToan, groupingSymbol, decimalSymbol, 0)));
                str = str.Replace("tienDatCocDangKy", StringHelper.Format(tt.GiaTriThanhToan, groupingSymbol, decimalSymbol, 0));
                str = str.Replace("ngayPhaiDatCocDangKy", tt.NgayDuKien != null ? string.Format("{0:dd/MM/yyyy}", tt.NgayDuKien).DateToText() : "");
            }
            else
            {
                str = str.Replace("tienDatCocBangChuDangKy", StringHelper.ReadNumber(StringHelper.Format(data.TienDatCoc, groupingSymbol, decimalSymbol, 0)));
                str = str.Replace("tienDatCocDangKy", StringHelper.Format(data.TienDatCoc, groupingSymbol, decimalSymbol, 0));
                str = str.Replace("ngayPhaiDatCocDangKy", data.NgayPhaiDatCoc != null ? string.Format("{0:dd/MM/yyyy}", data.NgayPhaiDatCoc).DateToText() : "");
            }
            str = str.Replace("ngayKyHopDongDangKy", data.NgayKyHopDongStr.DateToText());
            str = str.Replace("tyLeChietKhauDangKy", StringHelper.Format(data.TyLeChietKhau, groupingSymbol, decimalSymbol, decimalPlaces));
            if (data.SoTienChietKhau.HasValue)
                str = str.Replace("soTienChietKhauBangChuDangKy", StringHelper.ReadNumber(StringHelper.Format(data.SoTienChietKhau, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("soTienChietKhauBangChuDangKy", "");
            str = str.Replace("soTienChietKhauDangKy", StringHelper.Format(data.SoTienChietKhau, groupingSymbol, decimalSymbol, 0));
            string ndck = WordUltities.RemoveCodeHTML(data.NoiDungCamKet);
            str = str.Replace("noiDungCamKetDangKy", ndck);
            string gc = WordUltities.RemoveCodeHTML(data.GhiChu);
            str = str.Replace("ghiChuDangKy", gc);
            if (md == true)
            {
                if (data.DuAnInfo != null)
                    str = WordUltities.FillDuAn(str, data.DuAnInfo);
                if (data.BlockInfo != null)
                    str = WordUltities.FillBlock(str, data.BlockInfo);
                if (data.LoTangInfo != null)
                    str = WordUltities.FillTang(str, data.LoTangInfo);
                if (data.NenCanHoInfo != null)
                    str = WordUltities.FillSanPham(str, data.NenCanHoInfo);
                if (data.KhachDaiDienInfo != null)
                    str = WordUltities.FillKhachHang(str, data.KhachDaiDienInfo);
                if (data.PhuongThucThanhToanInfo != null)
                    str = WordUltities.FillPhuongThucThanhToan(str, data.PhuongThucThanhToanInfo);
            }
            return str;
        }
        /// <summary>
        /// Điền thông tin về hợp đồng
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillHopDong(string str, KDBHopDongInfo data, bool md)
        {

            #region Số và ngày ký hợp động
            str = str.Replace("soHopDong", string.IsNullOrEmpty(data.SoPhuLuc) ? data.SoHopDong : data.SoPhuLuc);
            str = str.Replace("ngayKyHopDong", string.Format("{0:dd/MM/yyyy}", data.NgayPhuLuc ?? data.NgayKy).DateToText());
            //str = str.Replace("soPhuLucHopDong", data.SoHopDong ?? "");
            //str = str.Replace("ngayKyPhuLucHopDong", string.Format("{0:dd/MM/yyyy}", data.NgayKy).DateToText());
            #endregion

            #region Điền thông tin về các loại hợp đồng
            // son.le-2011.09.07
            switch (data.LoaiNenCanHoTT)
            {

                case E_LoaiNenCanHo.S_Nen_Dat:
                    {
                        //Hợp đồng đặt cọc
                        str = WordUltities.FillHopDongDatCoc(str, data);
                        break;
                    }
                case E_LoaiNenCanHo.S_Nen_Mong:
                    {
                        //Hợp đồng tiến trình mua bán nhà ở hình thành tương lai
                        str = WordUltities.FillHopDongTienTrinh(str, data);
                        break;
                    }
                case E_LoaiNenCanHo.S_Can_Ho_Tho:
                case E_LoaiNenCanHo.S_Can_Ho_Hoan_Thien:
                case E_LoaiNenCanHo.S_Nha_Hoan_Thien:
                case E_LoaiNenCanHo.S_Nha_Tho:
                    {
                        //Hợp đồng mua bán nhà ở hình thành tương lai
                        str = WordUltities.FillHopDongTuongLai(str, data);
                        break;
                    }
                default:
                    break;
            }
            //str = WordUltities.FillHopDongTuongLai(str, data);
            //str = WordUltities.FillHopDongTienTrinh(str, data);
            //str = WordUltities.FillHopDongDatCoc(str, data);
            #endregion

            #region Diện tích
            var dienTichXayDung = data.DienTichXayDungTT.HasValue ? data.DienTichXayDungTT : (data.DienTichXayDung.HasValue ? data.DienTichXayDung.Value : 0);
            var dienTich = data.DienTichTT.HasValue ? data.DienTichTT.Value : (data.DienTich.HasValue ? data.DienTich.Value : 0);
            var dienTichMong = data.DienTichMongTT.HasValue ? data.DienTichMongTT.Value : (data.DienTichMong.HasValue ? data.DienTichMong.Value : 0);

            str = str.Replace("dienTichMongHopDong", StringHelper.Format(dienTichMong, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichHopDong", StringHelper.Format(dienTich, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichXayDungHopDong", StringHelper.Format(dienTichXayDung, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichSuDungChungHopDong", StringHelper.Format(data.DienTichSuDungChung, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichSuDungRiengHopDong", StringHelper.Format(data.DienTichSuDungRieng, groupingSymbol, decimalSymbol, decimalPlaces));
            #endregion

            #region Đơn giá
            var donGiaXayDung = data.DonGiaXayDungTT.HasValue ? data.DonGiaXayDungTT.Value : (data.DonGiaXayDung.HasValue ? data.DonGiaXayDung.Value : 0);
            var donGia = data.DonGiaTT.HasValue ? data.DonGiaTT.Value : (data.DonGia.HasValue ? data.DonGia.Value : 0);
            var donGiaMong = data.DonGiaMongTT.HasValue ? data.DonGiaMongTT.Value : (data.DonGiaMong.HasValue ? data.DonGiaMong.Value : 0);

            str = str.Replace("donGiaBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(donGia, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("donGiaHopDong", StringHelper.Format(donGia, groupingSymbol, decimalSymbol, 0));

            str = str.Replace("donGiaMongBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(donGiaMong, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("donGiaMongHopDong", StringHelper.Format(donGiaMong, groupingSymbol, decimalSymbol, 0));

            str = str.Replace("donGiaXayDungBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(donGiaXayDung, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("donGiaXayDungHopDong", StringHelper.Format(donGiaXayDung, groupingSymbol, decimalSymbol, 0));
            #endregion

            #region Giá trị khác
            var triGiaNen = data.TriGiaNenTT.HasValue ? data.TriGiaNenTT.Value : (data.TriGiaNen.HasValue ? data.TriGiaNen.Value : 0);
            str = str.Replace("giaTriNenBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(triGiaNen, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("giaTriNenHopDong", StringHelper.Format(triGiaNen, groupingSymbol, decimalSymbol, 0));
            var triGiaMong = data.TriGiaMongTT.HasValue ? data.TriGiaMongTT.Value : (data.TriGiaMong.HasValue ? data.TriGiaMong.Value : 0);
            str = str.Replace("giaTriMongBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(triGiaMong, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("giaTriMongHopDong", StringHelper.Format(triGiaMong, groupingSymbol, decimalSymbol, 0));

            var triGiaNha = data.TriGiaNhaTT.HasValue ? data.TriGiaNhaTT.Value : (data.TriGiaNha.HasValue ? data.TriGiaNha.Value : 0);
            str = str.Replace("giaTriNhaBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(triGiaNha, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("giaTriNhaHopDong", StringHelper.Format(triGiaNha, groupingSymbol, decimalSymbol, 0));
            #endregion

            #region Giá trị chưa VAT
            var giaTriChuaVAT = data.GiaTriHopDongTT.HasValue ? data.GiaTriHopDongTT.Value : (data.GiaTriHopDong.HasValue ? data.GiaTriHopDong.Value : 0);
            str = str.Replace("giaTriChuaVATBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(giaTriChuaVAT, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("giaTriChuaVATHopDong", StringHelper.Format(giaTriChuaVAT, groupingSymbol, decimalSymbol, 0));

            str = str.Replace("giaTriChuaVATBangChuDatCocHopDong", StringHelper.ReadNumber(StringHelper.Format(giaTriChuaVAT, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("giaTriChuaVATDatCocHopDong", StringHelper.Format(giaTriChuaVAT, groupingSymbol, decimalSymbol, 0));

            str = str.Replace("giaTriChuaVATBangChuTienTrinhHopDong", StringHelper.ReadNumber(StringHelper.Format(giaTriChuaVAT, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("giaTriChuaVATTienTrinhHopDong", StringHelper.Format(giaTriChuaVAT, groupingSymbol, decimalSymbol, 0));

            str = str.Replace("giaTriChuaVATBangChuTuongLaiHopDong", StringHelper.ReadNumber(StringHelper.Format(giaTriChuaVAT, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("giaTriChuaVATTuongLaiHopDong", StringHelper.Format(giaTriChuaVAT, groupingSymbol, decimalSymbol, 0));

            #endregion

            #region Giá trị hợp đồng. Lấy theo giá trị VAT
            //Sẽ lấy tương ứng với loại hợp đồng theo 3 hàm tương ứng bên dưới
            var giaTriCoVAT = data.GiaTriHopDongVATTT;
            str = str.Replace("giaTriCoVATBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(giaTriCoVAT, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("giaTriCoVATHopDong", StringHelper.Format(giaTriCoVAT, groupingSymbol, decimalSymbol, 0));
            #endregion

            #region Khác
            str = str.Replace("tyLeChietKhauHopDong", StringHelper.Format(data.TyLeChietKhau, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("soTienChietKhauHopDong", StringHelper.Format(data.SoTienChietKhau, groupingSymbol, decimalSymbol, 0));
            str = str.Replace("hinhThucThanhToanHopDong", data.HinhThucThanhToanStr);
            str = str.Replace("soGiayBaoLanhHopDong", data.SoGiayBaoLanh);
            str = str.Replace("ngayKyGiayBaoLanhHopDong", data.NgayKyGiayBaoLanh != null ? string.Format("{0:dd/MM/yyyy}", data.NgayKyGiayBaoLanh).DateToText() : "");
            str = str.Replace("nguoiKyGiayBaoLanhHopDong", data.NguoiKyGiayBaoLanh);
            str = str.Replace("ngayGiaoDuKienHopDong", data.NgayGiao != null ? string.Format("{0:dd/MM/yyyy}", data.NgayGiao).DateToText() : "");
            string ndck = WordUltities.RemoveCodeHTML(data.NoiDungCamKet);
            str = str.Replace("noiDungCamKetHopDong", ndck);
            string gcgc = WordUltities.RemoveCodeHTML(data.GhiChu);
            str = str.Replace("ghiChuHopDong", gcgc ?? "");
            str = str.Replace("thueVATHopDong", StringHelper.Format(data.ThueVAT, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("thueVATBangChuTienTrinhHopDong", StringHelper.ReadNumber(StringHelper.Format(giaTriChuaVAT, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("loaiSanPham", data.LoaiNenCanHoStr);
            #endregion

            #region Số tiền đã thanh toán, số tiền còn lại
            if (data.TongTienDaThu != 0)
                str = str.Replace("soTienDaThanhToanBangChu", StringHelper.ReadNumber(StringHelper.Format(data.TongTienDaThu, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("soTienDaThanhToanBangChu", "không ");
            str = str.Replace("soTienDaThanhToan", StringHelper.Format(data.TongTienDaThu, groupingSymbol, decimalSymbol, 0));
            double soTienConLai = data.GiaTriHopDongVATTT - data.TongTienDaThu ?? 0;
            soTienConLai = Math.Abs(soTienConLai);
            str = str.Replace("soTienConLaiBangChu", StringHelper.ReadNumber(StringHelper.Format(soTienConLai, groupingSymbol, decimalSymbol, 0)));
            str = str.Replace("soTienConLai", StringHelper.Format(soTienConLai, groupingSymbol, decimalSymbol, 0));
            #endregion

            #region temp
            //if (data.DienTich != null && data.DienTichTT != null)
            //{
            //    double dienTichChenhLech = data.DienTichTT.Value - data.DienTich.Value;
            //    dienTichChenhLech = Math.Abs(dienTichChenhLech);
            //    str = str.Replace("dienTichChenhLechHopDong", StringHelper.Format(dienTichChenhLech, groupingSymbol, decimalSymbol, decimalPlaces));
            //}
            //if (data.GiaTriHopDongVATConLai.HasValue)
            //    str = str.Replace("giaTriCoVATConLaiBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(data.GiaTriHopDongVATConLai, groupingSymbol, decimalSymbol, 0).ToString()));
            //else
            //    str = str.Replace("giaTriCoVATConLaiBangChuHopDong", "");
            //str = str.Replace("giaTriCoVATConLaiHopDong", StringHelper.Format(data.GiaTriHopDongVATConLai, groupingSymbol, decimalSymbol, 0));
            //if (data.DangKyInfo != null)
            //{
            //    str = str.Replace("tienCocBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(data.DangKyInfo.TienDatCoc, groupingSymbol, decimalSymbol, 0)));
            //    str = str.Replace("tienCocHopDong", StringHelper.Format(data.DangKyInfo.TienDatCoc, groupingSymbol, decimalSymbol, 0));
            //} 
            //str = str.Replace("tyLeGiamHopDong", StringHelper.Format(data.TyLeGiam, groupingSymbol, decimalSymbol, decimalPlaces));
            //if (data.TienGiam.HasValue)
            //    str = str.Replace("tienGiamBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(data.TienGiam, groupingSymbol, decimalSymbol, 0).ToString()));
            //else
            //    str = str.Replace("tienGiamBangChuHopDong", "");
            //str = str.Replace("tienGiamHopDong", StringHelper.Format(data.TienGiam, groupingSymbol, decimalSymbol, 0));

            #endregion
            #region --Ngày phủ mỹ hưng ký--
            str = str.Replace("ngayPMHKy", data.NgayPhuLucStr);
            #endregion
            #region Insert thông tin liên quan
            if (md == true)
            {
                if (data.GiuChoInfo != null)
                    str = WordUltities.FillPhieuGiuCho(str, data.GiuChoInfo, false);
                if (data.DangKyInfo != null)
                    str = WordUltities.FillPhieuDangKy(str, data.DangKyInfo, false);
                if (data.DuAnInfo != null)
                    str = WordUltities.FillDuAn(str, data.DuAnInfo);
                if (data.BlockInfo != null)
                    str = WordUltities.FillBlock(str, data.BlockInfo);
                if (data.LoTangInfo != null)
                    str = WordUltities.FillTang(str, data.LoTangInfo);
                if (data.NenCanHoInfo != null)
                    str = WordUltities.FillSanPham(str, data.NenCanHoInfo);
                if (data.KhachHangList != null)
                    for (int i = 0; i < data.KhachHangList.Count; i++)
                    {
                        if (data.KhachHangList[i].DaiDien == 'T')
                        {
                            str = WordUltities.FillKhachHang(str, data.KhachHangList[i].KhachHangInfo);
                            break;
                        }
                    }
                if (data.PhuongThucThanhToanInfo != null)
                    str = WordUltities.FillPhuongThucThanhToan(str, data.PhuongThucThanhToanInfo);
            }
            #endregion

            return str;
        }
        static double tongDatCocVaTienTrinh = 0;
        public static string FillHopDongDatCoc(string str, KDBHopDongBackupInfo data)
        {
            if (data != null)
            {
                var soHopDong = string.IsNullOrEmpty(data.SoPhuLuc) ? data.SoHopDong : data.SoPhuLuc;
                str = str.Replace("soDatCocHopDong", soHopDong ?? "");
                str = str.Replace("ngayKyDatCocHopDong", string.Format("{0:dd/MM/yyyy}", data.NgayKy).DateToText());

                var giaTriCoVAT = data.GiaTriHopDongVATTT;
                tongDatCocVaTienTrinh += giaTriCoVAT;
                str = str.Replace("giaTriCoVATBangChuDatCocHopDong", StringHelper.ReadNumber(StringHelper.Format(giaTriCoVAT, groupingSymbol, decimalSymbol, 0).ToString()));
                str = str.Replace("giaTriCoVATDatCocHopDong", StringHelper.Format(giaTriCoVAT, groupingSymbol, decimalSymbol, 0));
                //dùng cho hợp đồng tương lai
                str = str.Replace("tongDatCocVaTienTrinhHopDong", StringHelper.Format(tongDatCocVaTienTrinh, groupingSymbol, decimalSymbol, 0));
                str = str.Replace("tongBangChuDatCocVaTienTrinhHopDong", StringHelper.ReadNumber(StringHelper.Format(tongDatCocVaTienTrinh, groupingSymbol, decimalSymbol, 0)));
                //tongDatCocVaTienTrinhHopDong
            }
            else
            {
                str = str.Replace("soDatCocHopDong", "");
                str = str.Replace("ngayKyDatCocHopDong", "");
                str = str.Replace("giaTriCoVATBangChuDatCocHopDong", "");
                str = str.Replace("giaTriCoVATDatCocHopDong", "");
                str = str.Replace("tongDatCocVaTienTrinhHopDong", "");
                str = str.Replace("tongBangChuDatCocVaTienTrinhHopDong", "");
            }
            return str;
        }
        public static string FillHopDongDatCoc(string str, KDBHopDongInfo hopDong)
        {
            if (hopDong != null)
            {
                var soHopDong = string.IsNullOrEmpty(hopDong.SoPhuLuc) ? hopDong.SoHopDong : hopDong.SoPhuLuc;
                str = str.Replace("soDatCocHopDong", soHopDong ?? "");
                str = str.Replace("ngayKyDatCocHopDong", string.Format("{0:dd/MM/yyyy}", hopDong.NgayKy).DateToText());
                var giaTriCoVAT = hopDong.GiaTriHopDongVATTT;
                tongDatCocVaTienTrinh += giaTriCoVAT;
                str = str.Replace("giaTriCoVATBangChuDatCocHopDong", StringHelper.ReadNumber(StringHelper.Format(giaTriCoVAT, groupingSymbol, decimalSymbol, 0).ToString()));
                str = str.Replace("giaTriCoVATDatCocHopDong", StringHelper.Format(giaTriCoVAT, groupingSymbol, decimalSymbol, 0));
                //dùng cho hợp đồng tương lai
                str = str.Replace("tongDatCocVaTienTrinhHopDong", StringHelper.Format(tongDatCocVaTienTrinh, groupingSymbol, decimalSymbol, 0));
                str = str.Replace("tongBangChuDatCocVaTienTrinhHopDong", StringHelper.ReadNumber(StringHelper.Format(tongDatCocVaTienTrinh, groupingSymbol, decimalSymbol, 0)));
                //tongDatCocVaTienTrinhHopDong
            }
            else
            {
                str = str.Replace("soDatCocHopDong", "");
                str = str.Replace("ngayKyDatCocHopDong", "");
                str = str.Replace("giaTriCoVATBangChuDatCocHopDong", "");
                str = str.Replace("giaTriCoVATDatCocHopDong", "");
                str = str.Replace("tongDatCocVaTienTrinhHopDong", "");
                str = str.Replace("tongBangChuDatCocVaTienTrinhHopDong", "");
            }
            return str;
        }
        public static string FillHopDongTienTrinh(string str, KDBHopDongBackupInfo data)
        {
            if (data != null)
            {
                var soHopDong = string.IsNullOrEmpty(data.SoPhuLuc) ? data.SoHopDong : data.SoPhuLuc;
                str = str.Replace("soTienTrinhHopDong", soHopDong ?? "");
                str = str.Replace("ngayKyTienTrinhHopDong", string.Format("{0:dd/MM/yyyy}", data.NgayKy).DateToText());
                var giaTriCoVAT = data.GiaTriHopDongVATTT;
                tongDatCocVaTienTrinh = giaTriCoVAT;
                str = str.Replace("giaTriCoVATBangChuTienTrinhHopDong", StringHelper.ReadNumber(StringHelper.Format(giaTriCoVAT, groupingSymbol, decimalSymbol, 0).ToString()));
                str = str.Replace("giaTriCoVATTienTrinhHopDong", StringHelper.Format(giaTriCoVAT, groupingSymbol, decimalSymbol, 0));
            }
            else
            {
                str = str.Replace("soTienTrinhHopDong", "");
                str = str.Replace("ngayKyTienTrinhHopDong", "");
                str = str.Replace("giaTriCoVATBangChuTienTrinhHopDong", "");
                str = str.Replace("giaTriCoVATTienTrinhHopDong", "");
            }
            return str;
        }
        public static string FillHopDongTienTrinh(string str, KDBHopDongInfo data)
        {
            if (data != null)
            {
                var soHopDong = string.IsNullOrEmpty(data.SoPhuLuc) ? data.SoHopDong : data.SoPhuLuc;
                str = str.Replace("soTienTrinhHopDong", soHopDong ?? "");
                str = str.Replace("ngayKyTienTrinhHopDong", string.Format("{0:dd/MM/yyyy}", data.NgayKy).DateToText());
                var giaTriCoVAT = data.GiaTriHopDongVATTT;
                tongDatCocVaTienTrinh = giaTriCoVAT;
                str = str.Replace("giaTriCoVATBangChuTienTrinhHopDong", StringHelper.ReadNumber(StringHelper.Format(giaTriCoVAT, groupingSymbol, decimalSymbol, 0).ToString()));
                str = str.Replace("giaTriCoVATTienTrinhHopDong", StringHelper.Format(giaTriCoVAT, groupingSymbol, decimalSymbol, 0));
            }
            else
            {
                str = str.Replace("soTienTrinhHopDong", "");
                str = str.Replace("ngayKyTienTrinhHopDong", "");
                str = str.Replace("giaTriCoVATBangChuTienTrinhHopDong", "");
                str = str.Replace("giaTriCoVATTienTrinhHopDong", "");
            }
                return str;
        }
        public static string FillHopDongTuongLai(string str, KDBHopDongBackupInfo data)
        {
            tongDatCocVaTienTrinh = 0;
            var soHopDong = string.IsNullOrEmpty(data.SoPhuLuc) ? data.SoHopDong : data.SoPhuLuc;
            str = str.Replace("soTuongLaiHopDong", soHopDong ?? "");
            str = str.Replace("ngayKyTuongLaiHopDong", string.Format("{0:dd/MM/yyyy}", data.NgayKy).DateToText());
            var giaTriCoVAT = data.GiaTriHopDongVATTT;
            str = str.Replace("giaTriCoVATBangChuTuongLaiHopDong", StringHelper.ReadNumber(StringHelper.Format(giaTriCoVAT, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("giaTriCoVATTuongLaiHopDong", StringHelper.Format(giaTriCoVAT, groupingSymbol, decimalSymbol, 0));
            return str;
        }
        public static string FillHopDongTuongLai(string str, KDBHopDongInfo data)
        {
            tongDatCocVaTienTrinh = 0;
            var soHopDong = string.IsNullOrEmpty(data.SoPhuLuc) ? data.SoHopDong : data.SoPhuLuc;
            str = str.Replace("soTuongLaiHopDong", soHopDong ?? "");
            str = str.Replace("ngayKyTuongLaiHopDong", string.Format("{0:dd/MM/yyyy}", data.NgayKy).DateToText());
            var giaTriCoVAT = data.GiaTriHopDongVATTT;
            str = str.Replace("giaTriCoVATBangChuTuongLaiHopDong", StringHelper.ReadNumber(StringHelper.Format(giaTriCoVAT, groupingSymbol, decimalSymbol, 0).ToString()));
            str = str.Replace("giaTriCoVATTuongLaiHopDong", StringHelper.Format(giaTriCoVAT, groupingSymbol, decimalSymbol, 0));

            return str;
        }

        /// <summary>
        /// Điền thông tin về hợp đồng backup (Phụ lục hợp đồng)
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillHopDongBackup(string str, KDBHopDongBackupInfo data, bool md)
        {
            str = str.Replace("maHopDong", data.MaHopDong ?? "");
            str = str.Replace("soHopDong", data.SoHopDong ?? "");
            str = str.Replace("ngayKyHopDong", string.Format("{0:dd/MM/yyyy}", data.NgayKy).DateToText());
            str = str.Replace("loaiSanPham", data.LoaiNenCanHoStr);
            str = str.Replace("dienTichHopDong", StringHelper.Format(data.DienTich, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichThucTeHopDong", StringHelper.Format(data.DienTichTT, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichXayDungHopDong", StringHelper.Format(data.DienTichXayDung, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichXayDungThucTeHopDong", StringHelper.Format(data.DienTichXayDungTT, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichSuDungChungHopDong", StringHelper.Format(data.DienTichSuDungChung, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("dienTichSuDungRiengHopDong", StringHelper.Format(data.DienTichSuDungRieng, groupingSymbol, decimalSymbol, decimalPlaces));
            if (data.DienTich != null && data.DienTichTT != null)
            {
                double dienTichChenhLech = data.DienTichTT.Value - data.DienTich.Value;
                dienTichChenhLech = Math.Abs(dienTichChenhLech);
                str = str.Replace("dienTichChenhLechHopDong", StringHelper.Format(dienTichChenhLech, groupingSymbol, decimalSymbol, decimalPlaces));
            }
            if (data.DonGia.HasValue)
                str = str.Replace("donGiaBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(data.DonGia, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("donGiaBangChuHopDong", "");
            str = str.Replace("donGiaHopDong", StringHelper.Format(data.DonGia, groupingSymbol, decimalSymbol, 0));
            if (data.DonGiaTT.HasValue)
                str = str.Replace("donGiaThucTeBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(data.DonGiaTT, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("donGiaThucTeBangChuHopDong", "");
            str = str.Replace("donGiaThucTeHopDong", StringHelper.Format(data.DonGiaTT, groupingSymbol, decimalSymbol, 0));
            if (data.DonGiaXayDung.HasValue)
                str = str.Replace("donGiaXayDungBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(data.DonGiaXayDung, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("donGiaXayDungBangChuHopDong", "");
            str = str.Replace("donGiaXayDungHopDong", StringHelper.Format(data.DonGiaXayDung, groupingSymbol, decimalSymbol, 0));
            if (data.DonGiaXayDung.HasValue)
                str = str.Replace("donGiaXayDungThucTeBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(data.DonGiaXayDung, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("donGiaXayDungThucTeBangChuHopDong", "");
            str = str.Replace("donGiaXayDungThucTeHopDong", StringHelper.Format(data.DonGiaXayDung, groupingSymbol, decimalSymbol, 0));
            if (data.TriGiaNen.HasValue)
                str = str.Replace("giaTriNenHopDong", StringHelper.ReadNumber(StringHelper.Format(data.TriGiaNen, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("giaTriNenHopDong", "");
            str = str.Replace("giaTriNenHopDong", StringHelper.Format(data.TriGiaNen, groupingSymbol, decimalSymbol, 0));
            if (data.TriGiaNenTT.HasValue)
                str = str.Replace("giaTriNenThucTeBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(data.TriGiaNenTT, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("giaTriNenThucTeBangChuHopDong", "");
            str = str.Replace("giaTriNenThucTeHopDong", StringHelper.Format(data.TriGiaNenTT, groupingSymbol, decimalSymbol, 0));
            if (data.TriGiaNha.HasValue)
                str = str.Replace("giaTriNhaBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(data.TriGiaNha, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("giaTriNhaBangChuHopDong", "");
            str = str.Replace("giaTriNhaHopDong", StringHelper.Format(data.TriGiaNha, groupingSymbol, decimalSymbol, 0));
            if (data.TriGiaNhaTT.HasValue)
                str = str.Replace("giaTriNhaThucTeBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(data.TriGiaNhaTT, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("giaTriNhaThucTeBangChuHopDong", "");
            str = str.Replace("giaTriNhaThucTeHopDong", StringHelper.Format(data.TriGiaNhaTT, groupingSymbol, decimalSymbol, 0));
            if (data.TongGiaTri.HasValue)
                str = str.Replace("tongGiaTriBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(data.TongGiaTri, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("tongGiaTriBangChuHopDong", "");
            str = str.Replace("tongGiaTriHopDong", StringHelper.Format(data.TongGiaTri, groupingSymbol, decimalSymbol, 0));
            if (data.TongGiaTriTT.HasValue)
                str = str.Replace("tongGiaTriThucTeBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(data.TongGiaTriTT, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("tongGiaTriThucTeBangChuHopDong", "");
            str = str.Replace("tongGiaTriThucTeHopDong", StringHelper.Format(data.TongGiaTriTT, groupingSymbol, decimalSymbol, 0));
            str = str.Replace("tyLeGiamHopDong", StringHelper.Format(data.TyLeGiam, groupingSymbol, decimalSymbol, decimalPlaces));
            if (data.TienGiam.HasValue)
                str = str.Replace("tienGiamBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(data.TienGiam, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("tienGiamBangChuHopDong", "");
            str = str.Replace("tienGiamHopDong", StringHelper.Format(data.TienGiam, groupingSymbol, decimalSymbol, 0));
            if (data.GiaTriHopDong.HasValue)
                str = str.Replace("giaTriChuaVATBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(data.GiaTriHopDong, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("giaTriChuaVATBangChuHopDong", "");
            str = str.Replace("giaTriChuaVATHopDong", StringHelper.Format(data.GiaTriHopDong, groupingSymbol, decimalSymbol, 0));
            if (data.GiaTriHopDongVAT.HasValue)
                str = str.Replace("giaTriCoVATBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(data.GiaTriHopDongVAT, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("giaTriCoVATBangChuHopDong", "");
            str = str.Replace("giaTriCoVATHopDong", StringHelper.Format(data.GiaTriHopDongVAT, groupingSymbol, decimalSymbol, 0));
            if (data.GiaTriHopDongVATConLai.HasValue)
                str = str.Replace("giaTriCoVATConLaiBangChuHopDong", StringHelper.ReadNumber(StringHelper.Format(data.GiaTriHopDongVATConLai, groupingSymbol, decimalSymbol, 0).ToString()));
            else
                str = str.Replace("giaTriCoVATConLaiBangChuHopDong", "");
            str = str.Replace("giaTriCoVATConLaiHopDong", StringHelper.Format(data.GiaTriHopDongVATConLai, groupingSymbol, decimalSymbol, 0));
            str = str.Replace("thueVATHopDong", StringHelper.Format(data.ThueVAT, groupingSymbol, decimalSymbol, decimalPlaces));
            if (data.DangKyInfo != null)
                str = str.Replace("tienCocHopDong", StringHelper.Format(data.DangKyInfo.TienDatCoc, groupingSymbol, decimalSymbol, 0));
            str = str.Replace("tyLeChietKhauHopDong", StringHelper.Format(data.TyLeChietKhau, groupingSymbol, decimalSymbol, decimalPlaces));
            str = str.Replace("soTienChietKhauHopDong", StringHelper.Format(data.SoTienChietKhau, groupingSymbol, decimalSymbol, 0));
            str = str.Replace("soGiayBaoLanhHopDong", data.SoGiayBaoLanh);
            str = str.Replace("ngayKyGiayBaoLanhHopDong", data.NgayKyGiayBaoLanh != null ? string.Format("{0:dd/MM/yyyy}", data.NgayKyGiayBaoLanh).DateToText() : "");
            str = str.Replace("nguoiKyGiayBaoLanhHopDong", data.NguoiKyGiayBaoLanh);
            str = str.Replace("ngayGiaoDuKienHopDong", data.NgayGiao != null ? string.Format("{0:dd/MM/yyyy}", data.NgayGiao).DateToText() : "");
            string ndck = WordUltities.RemoveCodeHTML(data.NoiDungCamKet);
            str = str.Replace("noiDungCamKetHopDong", ndck);
            string gcgc = WordUltities.RemoveCodeHTML(data.GhiChu);
            str = str.Replace("ghiChuHopDong", gcgc ?? "");
            if (md == true)
            {
                if (data.GiuChoInfo != null)
                    str = WordUltities.FillPhieuGiuCho(str, data.GiuChoInfo, false);
                if (data.DangKyInfo != null)
                    str = WordUltities.FillPhieuDangKy(str, data.DangKyInfo, false);
                if (data.DuAnInfo != null)
                    str = WordUltities.FillDuAn(str, data.DuAnInfo);
                if (data.BlockInfo != null)
                    str = WordUltities.FillBlock(str, data.BlockInfo);
                if (data.LoTangInfo != null)
                    str = WordUltities.FillTang(str, data.LoTangInfo);
                if (data.NenCanHoInfo != null)
                    str = WordUltities.FillSanPham(str, data.NenCanHoInfo);
                if (data.HopDongKhachHangBackupList != null)
                    for (int i = 0; i < data.HopDongKhachHangBackupList.Count; i++)
                    {
                        if (data.HopDongKhachHangBackupList[i].DaiDien == 'T')
                        {
                            str = WordUltities.FillKhachHang(str, data.HopDongKhachHangBackupList[i].KhachHangInfo);
                            break;
                        }
                    }
                if (data.PhuongThucThanhToanInfo != null)
                    str = WordUltities.FillPhuongThucThanhToan(str, data.PhuongThucThanhToanInfo);
            }
            return str;
        }
        /// <summary>
        /// Điền thông tin về văn bản chuyển nhượng
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillChuyenNhuongHopDong(string str, KDBHopDongChuyenNhuongInfo data)
        {
            if (data != null)
            {
                str = str.Replace("lanChuyenNhuong", data.LanChuyenNhuong.ToString());
                str = str.Replace("soVanBanChuyenNhuong", data.VanBanChuyenNhuong);
                str = str.Replace("ngayKyChuyenNhuong", data.NgayKyChuyenNhuongStr.DateToText());
                if (data.SoTienDaNop.HasValue)
                    str = str.Replace("soTienDaNopBangChu", StringHelper.ReadNumber(StringHelper.Format(data.SoTienDaNop, groupingSymbol, decimalSymbol, 0)));
                else
                    str = str.Replace("soTienDaNopBangChu", "");
                str = str.Replace("soTienDaNop", StringHelper.Format(data.SoTienDaNop, groupingSymbol, decimalSymbol, 0));
                str = str.Replace("tyLeDaNop", StringHelper.Format(data.TyLeDaNop, groupingSymbol, decimalSymbol, decimalPlaces));
                if (data.SoTienChenhLech.HasValue)
                    str = str.Replace("soTienChenhLechBangChu", StringHelper.ReadNumber(StringHelper.Format(data.SoTienChenhLech, groupingSymbol, decimalSymbol, 0)));
                else
                    str = str.Replace("soTienChenhLechBangChu", "");
                str = str.Replace("soTienChenhLech", StringHelper.Format(data.SoTienChenhLech, groupingSymbol, decimalSymbol, 0));
                //if (data.SoTienKhac.HasValue)
                str = str.Replace("soTienKhacBangChu", StringHelper.ReadNumber(StringHelper.Format(data.SoTienKhac, groupingSymbol, decimalSymbol, 0)));
                //else
                //str = str.Replace("soTienKhacBangChu", "");
                str = str.Replace("soTienKhac", StringHelper.Format(data.SoTienKhac, groupingSymbol, decimalSymbol, 0));
                if (data.GiaTriChuyenNhuong.HasValue)
                    str = str.Replace("giaTriChuyenNhuongBangChu", StringHelper.ReadNumber(StringHelper.Format(data.GiaTriChuyenNhuong, groupingSymbol, decimalSymbol, 0)));
                else
                    str = str.Replace("giaTriChuyenNhuongBangChu", "");
                str = str.Replace("giaTriChuyenNhuong", StringHelper.Format(data.GiaTriChuyenNhuong, groupingSymbol, decimalSymbol, 0));
                str = str.Replace("ngayThanhToanTiep", data.NgayThanhToanTiep != null ? string.Format("{0:dd/MM/yyyy}", data.NgayThanhToanTiep).DateToText() : "");
                if (data.PhiChuyenNhuong.HasValue)
                    str = str.Replace("phiChuyenNhuongBangChu", StringHelper.ReadNumber(StringHelper.Format(data.PhiChuyenNhuong, groupingSymbol, decimalSymbol, 0)));
                else
                    str = str.Replace("phiChuyenNhuongBangChu", "");
                str = str.Replace("phiChuyenNhuong", StringHelper.Format(data.PhiChuyenNhuong, groupingSymbol, decimalSymbol, 0));
                str = str.Replace("tyLeChuyenNhuong", StringHelper.Format(data.TyLeChuyenNhuong == null ? 0 : data.TyLeChuyenNhuong, groupingSymbol, decimalSymbol, decimalPlaces));
            }
            else
            {
                str = str.Replace("lanChuyenNhuong", "");
                str = str.Replace("soVanBanChuyenNhuong", "");
                str = str.Replace("ngayKyChuyenNhuong", "");
                str = str.Replace("soTienDaNopBangchu", "");
                str = str.Replace("soTienDaNop", "");
                str = str.Replace("tyLeDaNop", "");
                str = str.Replace("soTienChenhLechBangChu", "");
                str = str.Replace("soTienChenhLech", "");
                str = str.Replace("soTienKhacBangChu", "");
                str = str.Replace("soTienKhac", "");
                str = str.Replace("giaTriChuyenNhuongBangChu", "");
                str = str.Replace("giaTriChuyenNhuong", "");
                str = str.Replace("ngayThanhToanTiep", "");
                str = str.Replace("phiChuyenNhuong", "");
                str = str.Replace("tyLeChuyenNhuong", "");
            }
            return str;
        }
        /// <summary>
        /// Điền thông tin biên bản các loại bàn giao
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillBienBanBanGiao(string str, KDBHopDongBanGiaoInfo data)
        {
            if (data.DaBanGiao != null)
            {
                str = str.Replace("soBienBanBanGiao", data.DaBanGiao.SoBienBan);
                str = str.Replace("ngayBanGiao", string.Format("{0:dd/MM/yyyy}", data.DaBanGiao.NgayBanGiao).DateToText());
                str = str.Replace("tenNguoiBanGiao", data.DaBanGiao.TenNguoiBanGiao);
                str = str.Replace("tenNguoiNhanBanGiao", data.DaBanGiao.TenNguoiNhan);
                string nd = WordUltities.RemoveCodeHTML(data.DaBanGiao.NoiDung);
                str = str.Replace("noiDungBanGiao", nd);
                string ck = WordUltities.RemoveCodeHTML(data.DaBanGiao.CamKet);
                str = str.Replace("camKetBanGiao", ck);
                string gc = WordUltities.RemoveCodeHTML(data.GhiChu);
                str = str.Replace("ghiChuBanGiao", gc);
            }
            str = str.Replace("ngayDuKienBanGiao", data.NgayDuKienSTr.DateToText());
            str = str.Replace("loaiBanGiao", data.LoaiBanGiao);
            if (data.HopDong != null)
            {
                str = WordUltities.FillHopDong(str, data.HopDong, true);
            }
            return str;
        }
        /// <summary>
        /// Điền thông tin hợp đồng thanh lý kết thúc/trả lại
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillThanhLyHopDong(string str, KDBHopDongThanhLyInfo data)
        {
            if (data != null)
            {
                str = str.Replace("loaiThanhLy", data.LoaiStr);
                str = str.Replace("soThanhLy", data.SoThanhLy);
                str = str.Replace("ngayThanhLy", data.NgayThanhLyStr.DateToText());
                str = str.Replace("noiDungThanhLy", data.NoiDung);
                str = str.Replace("tyLeThanhLy", StringHelper.Format(data.TyLeThanhLy, groupingSymbol, decimalSymbol, decimalPlaces));
                if (data.SoTienThanhLy.HasValue)
                    str = str.Replace("tienThanhLyBangChu", StringHelper.ReadNumber(StringHelper.Format(data.SoTienThanhLy, groupingSymbol, decimalSymbol, 0)));
                else
                    str = str.Replace("tienThanhLyBangChu", "");
                str = str.Replace("tienThanhLy", StringHelper.Format(data.SoTienThanhLy, groupingSymbol, decimalSymbol, 0));
                if (data.SoTienHoanTra.HasValue)
                    str = str.Replace("SoTienHoanTra", StringHelper.Format(data.SoTienHoanTra, groupingSymbol, decimalSymbol, 0));
                else
                    str = str.Replace("SoTienHoanTra", "");


                if (data.SoTienHoanTra.HasValue)
                    str = str.Replace("SoTienHoanTraBangChu", StringHelper.ReadNumber(StringHelper.Format(data.SoTienHoanTra, groupingSymbol, decimalSymbol, 0)));
                else
                    str = str.Replace("SoTienHoanTraBangChu", "");
                if (data.TienPhat.HasValue)
                    str = str.Replace("SoTienPhat", StringHelper.ReadNumber(StringHelper.Format(data.TienPhat, groupingSymbol, decimalSymbol, 0)));
                else
                    str = str.Replace("SoTienPhat", "");

                if (data.TienPhat.HasValue)
                    str = str.Replace("SoTienPhatBangChu", StringHelper.ReadNumber(StringHelper.Format(data.TienPhat, groupingSymbol, decimalSymbol, 0)));
                else
                    str = str.Replace("SoTienPhatBangChu", "");
            }
            else
            {
                str = str.Replace("loaiThanhLy", "");
                str = str.Replace("soThanhLy", "");
                str = str.Replace("ngayThanhLy", "");
                str = str.Replace("noiDungThanhLy", "");
                str = str.Replace("tyLeThanhLy", "");
                str = str.Replace("tienThanhLyBangChu", "");
                str = str.Replace("tienThanhLy", "");
            }
            return str;
        }
        /// <summary>
        /// Điền thông tin thư mời thanh toán
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillThuMoiThanhToan(string str, KDBThanhToanThuMoiInfo data)
        {
            str = str.Replace("lanMoi", data.LanMoi != null ? data.LanMoi.Value.ToString() : "");
            str = str.Replace("soThuMoi", data.SoThuMoi);
            str = str.Replace("ngayThongBao", string.Format("{0:dd/MM/yyyy}", DateTime.Now).DateToText());
            return str;
        }
        /// <summary>
        /// Điền thông tin thư mời các loại bàn giao
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillThuMoiBanGiao(string str, KDBBanGiaoThuMoiInfo data)
        {
            str = str.Replace("lanMoi", data.LanMoi != null ? data.LanMoi.Value.ToString() : "");
            str = str.Replace("soThuMoi", data.SoThuMoi);
            //str = str.Replace("noiDung", data.NoiDung);
            str = str.Replace("ngayThongBao", string.Format("{0:dd/MM/yyyy}", DateTime.Now).DateToText());// data.NgayThucHien != null ? string.Format("{0:dd/MM/yyyy}", data.NgayThucHien) : "");
            if (data.LoaiBanGiaoInfo != null)
                str = str.Replace("loaiBanGiao", data.LoaiBanGiaoInfo.Ten);
            return str;
        }
        /// <summary>
        /// Điền thông tin của khách hàng chuyển nhượng
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <param name="paraCN">
        /// 0. Khách hàng chuyển nhượng
        /// 1. Khách hàng nhận chuyển nhượng
        /// </param>
        /// <returns></returns>
        public static string FillKhachHangChuyenNhuong(string str, DMKhachHangInfo data, int paraCN)
        {
            string prefix = paraCN == 0 ? "CN" : "NCN";
            str = str.Replace("tenKhachHang" + prefix, string.IsNullOrEmpty(data.HoTenKhachHang) == false ? data.HoTenKhachHang : "");
            str = str.Replace("ngaySinhKhachHang" + prefix, data.NgaySinh != null ? string.Format("{0:dd/MM/yyyy}", data.NgaySinh).DateToText() : "");
            str = str.Replace("gioiTinhKhachHang" + prefix, string.IsNullOrEmpty(data.GioiTinhStr) == false ? data.GioiTinhStr : "");
            str = str.Replace("quocTichKhachHang" + prefix, string.IsNullOrEmpty(data.QuocTich) == false ? data.QuocTich : "");
            str = str.Replace("cmndSoKhachHang" + prefix, string.IsNullOrEmpty(data.CmndSo) == false ? data.CmndSo : "");
            str = str.Replace("cmndNgayCapKhachHang" + prefix, string.IsNullOrEmpty(data.CmndNgayCapStr) == false ? data.CmndNgayCapStr : "");
            str = str.Replace("cmndNoiCapKhachHang" + prefix, string.IsNullOrEmpty(data.CmndNoiCap) == false ? data.CmndNoiCap : "");
            string dcc = WordUltities.RemoveCodeHTML(data.DiaChi);
            str = str.Replace("diaChiKhachHang" + prefix, string.IsNullOrEmpty(dcc) == false ? dcc : "");
            string tt = WordUltities.RemoveCodeHTML(data.ThuongTru);
            str = str.Replace("thuongTruKhachHang" + prefix, string.IsNullOrEmpty(tt) == false ? tt : "");
            str = str.Replace("dienThoaiKhachHang" + prefix, string.IsNullOrEmpty(data.DienThoai) == false ? data.DienThoai : "");
            str = str.Replace("faxKhachHang" + prefix, string.IsNullOrEmpty(data.Fax) == false ? data.Fax : "");
            str = str.Replace("diDongKhachHang" + prefix, string.IsNullOrEmpty(data.DiDong) == false ? data.DiDong : "");
            str = str.Replace("emailKhachHang" + prefix, string.IsNullOrEmpty(data.Email) == false ? data.Email : "");
            str = str.Replace("taiKhoanKhachHang" + prefix, string.IsNullOrEmpty(data.TaiKhoan) == false ? data.TaiKhoan : "");
            str = str.Replace("maSoThueKhachHang" + prefix, string.IsNullOrEmpty(data.MaSoThue) == false ? data.MaSoThue : "");
            //str = str.Replace("ngheNghiepKhachHang" + prefix, string.IsNullOrEmpty(data.NgheNghiep) == false ? data.NgheNghiep : "");
            str = str.Replace("ngheNghiepKhachHang" + prefix, string.IsNullOrEmpty(data.IdNgheNghiep.ToString()) == false ? data.IdNgheNghiep.ToString() : "");
            return str;
        }
        /// <summary>
        /// Phiếu thu
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillPhieuThu(string str, KTPhieuThuInfo data)
        {
            str = str.Replace("loaiPhieuThu", data.LoaiThuStr);
            str = str.Replace("ngayThu", data.NgayThuStr.DateToText(true));
            str = str.Replace("soPhieu", data.SoPhieu);
            //str = str.Replace("soNgayChoPhepTre", data.SoNgayChoPhepTreCuaGiaiDoan != null ? data.SoNgayChoPhepTreCuaGiaiDoan.Value.ToString() : "");
            str = str.Replace("soTienNopBangChu", data.TongThu != null ? StringHelper.ReadNumber(data.TongThu.Value.ToString()) : "");
            str = str.Replace("soTienNop", data.TongThu != null ? StringHelper.Format(data.TongThu, groupingSymbol, decimalSymbol, 0) : "");
            str = str.Replace("lyDoNop", RemoveCodeHTML(data.LyDo));
            str = str.Replace("noiDungNop", RemoveCodeHTML(data.NoiDung));
            str = str.Replace("ghiChu", RemoveCodeHTML(data.GhiChu));
            str = str.Replace("tenNguoiNop", data.NguoiNopInfo.HoTenKhachHang);
            str = str.Replace("diaChiNguoiNop", data.NguoiNopInfo.DiaChi.RemoveCodeHTML());
            str = str.Replace("tenNguoiLapPhieu", data.NguoiThu);
            return str;
        }

        public static string FillPhieuDeNghiThu(string str, KTDeNghiInfo data)
        {
            str = str.Replace("ngayDeNghiThu", data.NgayThuStr.DateToText(true));
            str = str.Replace("soPhieu", data.SoDeNghi);
            str = str.Replace("soTienNopBangChu", data.TongThu != null ? StringHelper.ReadNumber(data.TongThu.Value.ToString()) : "");
            str = str.Replace("soTienNop", data.TongThu != null ? StringHelper.Format(data.TongThu, groupingSymbol, decimalSymbol, 0) : "");
            str = str.Replace("lyDoNop", RemoveCodeHTML(data.LyDo));
            str = str.Replace("noiDungNop", RemoveCodeHTML(data.LoaiDeNghiStr));
            str = str.Replace("ghiChu", RemoveCodeHTML(data.GhiChu));
            str = str.Replace("tenNguoiNop", data.HoTenNguoiThucHien);
            str = str.Replace("tenNguoiLapPhieu", data.NguoiThu);
            return str;
        }

        /// <summary>
        /// Phiếu chi
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FillPhieuChi(string str, KTPhieuChiInfo data)
        {
            str = str.Replace("ngayChi", data.NgayChiStr.DateToText(true));
            str = str.Replace("soPhieu", data.SoPhieu);
            str = str.Replace("soTienChiBangChu", data.SoTien != null ? StringHelper.ReadNumber(data.SoTien.Value.ToString()) : "");
            str = str.Replace("soTienChi", data.SoTien != null ? StringHelper.Format(data.SoTien, groupingSymbol, decimalSymbol, 0) : "");
            str = str.Replace("noiDungChi", data.NoiDung.RemoveCodeHTML());
            str = str.Replace("ghiChu", data.GhiChu.RemoveCodeHTML());
            str = str.Replace("tenNguoiNhan", data.NguoiNhan);
            str = str.Replace("diaChiNguoiNhan", data.DiaChi.RemoveCodeHTML());
            str = str.Replace("tenNguoiLapPhieu", data.NguoiChi);
            return str;
        }

        public static string FillXacNhan(string str, KDBHopDongInfo data)
        {
            str = str.Replace("tongTienDaNopBangChu", StringHelper.ReadNumber(StringHelper.Format(data.TongTienDaThu, WordUltities.groupingSymbol, WordUltities.decimalSymbol, 0)));
            str = str.Replace("tongTienDaNop", StringHelper.Format(data.TongTienDaThu, WordUltities.groupingSymbol, WordUltities.decimalSymbol, 0));

            return str;
        }
        public static string FillGiaiDoanChoThuMoiThanhToan(string str, KDBHopDongInfo data)
        {
            if (data.GiaiDoanThanhToanList != null)
            {
                var indexGiaiDoanTiepTheo = data.GiaiDoanThanhToanList.Count - 1;
                #region Điền thông tin giai đoạn thanh toán tiếp theo

                var tempData = data.GiaiDoanThanhToanList[indexGiaiDoanTiepTheo];
                str = str.Replace("tenGDTT", tempData.GiaiDoan.ToString());
                str = str.Replace("ngayThuGDTT", tempData.NgayDuKienThuStr.DateToText());
                str = str.Replace("giaTriThanhToanGDTT", StringHelper.Format(tempData.GiaTriThanhToan, groupingSymbol, decimalSymbol, 0));
                str = str.Replace("giaTriThanhToanBangChuGDTT", StringHelper.ReadNumber(tempData.GiaTriThanhToan.ToString()));
                str = str.Replace("soTienDaThuGDTT", StringHelper.Format(tempData.TongTienDaThuGiaiDoan ?? 0, groupingSymbol, decimalSymbol, 0));
                str = str.Replace("soTienDaThuBangChuGDTT", StringHelper.ReadNumber((tempData.TongTienDaThuGiaiDoan ?? 0).ToString()));
                var soTienConLai = tempData.GiaTriThanhToan;
                if (tempData.TongTienDaThuGiaiDoan != null)
                    soTienConLai = Math.Abs(tempData.GiaTriThanhToan - tempData.TongTienDaThuGiaiDoan.Value);
                str = str.Replace("soTienConLaiGDTT", StringHelper.Format(soTienConLai, groupingSymbol, decimalSymbol, 0));
                str = str.Replace("soTienConLaiBangChuGDTT", StringHelper.ReadNumber(soTienConLai.ToString()));

                double laiSuat = tempData.LaiSuatTre??0;
                laiSuat = laiSuat / 100;
                //Ko lai
                int choPhepTre = tempData.ChoPhepTre ?? 0;
                int soNgayTre = DateTime.Now.Subtract(tempData.NgayGiaHan ?? tempData.NgayDuKien).Days;
                if (AppConfigModel.AddLate == 'T')
                    soNgayTre = (soNgayTre - choPhepTre) > 0 ? (soNgayTre - choPhepTre) : 0;
                double laiNganHang =Math.Round(((laiSuat * soTienConLai)/30) * soNgayTre,0);
                var tienPhat = tempData.TienPhat ?? 0;
                tienPhat = Math.Round(tienPhat * soTienConLai / 100, 0);
                var soTienPhaiThu = soTienConLai + laiNganHang + tienPhat;
                str = str.Replace("laiNganHangGDTT", StringHelper.Format(laiNganHang, groupingSymbol, decimalSymbol, 0));
                str = str.Replace("soTienPhatGDTT", StringHelper.Format(tienPhat, groupingSymbol, decimalSymbol, 0));
                var tyLeThanhToan = tempData.GiaTriThanhToan * 100 / data.GiaTriHopDongVATTT;
                str = str.Replace("tyLeThanhToanGDTT", StringHelper.Format(tyLeThanhToan, groupingSymbol, decimalSymbol, decimalPlaces));
                str = str.Replace("soTienPhaiThuBangChuGDTT", StringHelper.ReadNumber(soTienPhaiThu.ToString()));
                str = str.Replace("soTienPhaiThuGDTT", StringHelper.Format(soTienPhaiThu, groupingSymbol, decimalSymbol, 0));
                //str = str.Replace("tenDuAn", StringHelper.Format(1000000, groupingSymbol, decimalSymbol, 0));
                #endregion
            }
            else
            {
                str = str.Replace("tenGDTT", "");
                str = str.Replace("ngayThuGDTT", "");
                str = str.Replace("giaTriThanhToanGDTT", "");
                str = str.Replace("giaTriThanhToanBangChuGDTT", "");
                str = str.Replace("soTienDaThuGDTT", "");
                str = str.Replace("soTienDaThuBangChuGDTT", "");
                str = str.Replace("soTienConLaiGDTT", "");
                str = str.Replace("soTienConLaiBangChuGDTT", "");
                str = str.Replace("soNgayChoPhepTreGDTT", "");
                str = str.Replace("laiSuatGDTT", "");
            }
            return str;
        }
        public static ZipOutputStream CreateZipFile(string[] filesToZip, ref MemoryStream mem)
        {
            Crc32 crc = new Crc32();
            ZipOutputStream zipStream = new ZipOutputStream(mem);
            zipStream.SetLevel(9);
            // 0 - store only to 9 - means best compression
            for (int i = 0; i <= filesToZip.Length - 1; i++)
            {

                string fileName = Path.GetFileName(filesToZip[i]);
                ZipEntry entry = new ZipEntry(fileName);
                entry.DateTime = DateTime.Now;
                using (FileStream fileStream = System.IO.File.OpenRead(filesToZip[i]))
                {
                    byte[] buffer = new byte[fileStream.Length];
                    fileStream.Read(buffer, 0, buffer.Length);
                    entry.Size = fileStream.Length;
                    fileStream.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    zipStream.PutNextEntry(entry);
                    zipStream.Write(buffer, 0, buffer.Length);
                }
            }
            zipStream.Finish();
            return zipStream;
        }
        public static void LuuServer(string mauIn, string path)
        {
            StreamWriter writer = new StreamWriter(path);
            writer.Write(mauIn);
            writer.Close();
        }
        #endregion

        #region Send Mail
        /// <summary>
        /// Gởi email
        /// </summary>
        /// <param name="to">Địa chỉ người nhận</param>
        /// <param name="subject">Chủ đề</param>
        /// <param name="body">Nội dung thư gởi</param>
        /// <returns></returns>
        public static bool SendMail(string to, string subject, string body)
        {
            Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration("~/Web.config");
            MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
            if (mailSettings != null)
            {
                int port = 0;
                try
                {
                    port = mailSettings.Smtp.Network.Port;
                }
                catch
                {
                    port = 25;
                }
                try
                {
                    string host = mailSettings.Smtp.Network.Host;
                    string password = mailSettings.Smtp.Network.Password;
                    string username = mailSettings.Smtp.Network.UserName;
                    bool enableSSL = mailSettings.Smtp.Network.DefaultCredentials;
                    System.Net.Mail.MailMessage MailMesaji = new System.Net.Mail.MailMessage();
                    MailMesaji.Subject = subject;
                    MailMesaji.Headers.Add("Content-Type", "text/html");
                    MailMesaji.Body = body;
                    MailMesaji.IsBodyHtml = true;
                    MailMesaji.BodyEncoding = Encoding.UTF8;
                    MailMesaji.From = new MailAddress(username);
                    MailMesaji.To.Add(new MailAddress(to));
                    System.Net.Mail.SmtpClient Smtp = new SmtpClient();
                    Smtp.Host = host;
                    Smtp.Port = port;
                    if (enableSSL)
                    {
                        Smtp.EnableSsl = true;
                        Smtp.Credentials = new System.Net.NetworkCredential(username, password);
                        Smtp.Send(MailMesaji);
                    }
                    else
                        Smtp.Send(MailMesaji);
                    return true;
                }
                catch
                {
                    //throw;
                }
            }
            return false;
        }
        #endregion

        /// <summary>
        /// Xóa các ký hiệu trong dữ liệu nguồn
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string XoaKyHieuDuLieuNguon(this string str)
        {
            //while (str.Contains("&lt;&lt;"))
            //{
            //    var indexLT = str.IndexOf("&lt;&lt;");
            //    var indexGT = str.IndexOf("&gt;&gt;");
            //    if(indexGT > indexLT)
            //        str = str.Remove(indexLT, indexGT - indexLT + 8);
            //}
            return str;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="Upper">[0]: Viết hoa chữ cái đầu,
        /// [1]: Viết hoa các kí tự đầu
        /// [2]: Viết hoa tất cả
        /// </param>
        /// <returns></returns>
        public static string DateToText(this string str, params bool[] Upper)
        {
            if (string.IsNullOrEmpty(str))
                return "";
            var temp = str.Split(new string[] { "/" }, StringSplitOptions.None);
            if (temp.Length == 3)
            {
                var strTemp = "ngày {0} tháng {1} năm {2}";
                for (int i = 0; i < Upper.Length; i++)
                {
                    if (Upper[i])
                    {
                        if (i == 0)
                            strTemp = "Ngày {0} tháng {1} năm {2}";
                        if (i == 1)
                            strTemp = "Ngày {0} Tháng {1} Năm {2}";
                        if (i == 2)
                            strTemp = ("Ngày {0} tháng {1} năm {2}").ToUpper();
                    }
                }
                return string.Format(strTemp, temp[0], temp[1], temp[2]);
            }
            return "";
        }

        public static bool IsNullOrZero(this double? number)
        {
            if (number == null)
                return true;
            if (number == 0)
                return true;
            return false;
        }

        public static string ChuanHoaTenFile(this string str)
        {
            return str.Replace(" ", "_").Replace("/", "_").Replace("\\", "_").Replace("-", "_").Replace(".", "_").Replace(",", "_").Replace("(", "_").Replace(")", "_");
        }
    }
}
